Global ASAX - get the server name - asp.net

Can anybody tell me if there is a way for me to get the domain name of my site in the Application_Start event in the global.asax?
Normally I'd just get it from Context.Request.ServerVariables["SERVER_NAME"], but this is not available. I'd ideally also like to get the URL from the request that kicked off the application.
Hmm - from answers below, it would seem that being on IIS7 makes a difference here. This is new and there are now design guidelines to try and stop you from doing it:
IIS Blog

You can access the Context through the static HttpContext.Current member.
HttpContext.Current.Request.ServerVariables["SERVER_NAME"];
HttpContext.Current.Request.Url;
Edit, Based on some of your comments I did some additional research
This error is due to a design change in the IIS7 Integrated pipeline that makes the request context unavailable in Application_Start event. When using the Classic mode (the only mode when running on previous versions of IIS), the request context used to be available, even though the Application_Start event has always been intended as a global and request-agnostic event in the application lifetime. Despite this, because ASP.NET applications were always started by the first request to the app, it used to be possible to get to the request context through the static HttpContext.Current field.
So you have two options
Change your application code to not use the request context (recommended).
Move the application to Classic mode (NOT recommended).
http://mvolo.com/iis7-integrated-mode-request-is-not-available-in-this-context-exception-in-applicationstart/

Your web-application could run under multiple different domains. Since there is no current request in the Application_Start event, you cannot know under which domain the application will be called.
You could however find out the machine-name using System.Environment.MachineName.

I'm guessing you are on IIS 7? Because the HttpContext is available there on IIS 6.0.
Can you consider filling that information later on? The first call to Application_BeginRequest for example?

In VB.NET, in Global.asax, I use the following:
Hosting.HostingEnvironment.ApplicationHost.GetSiteName
It corresponds to the application name in IIS.
UPDATE: It seems the method "GetSiteName" is not intended to be called directly and it doesn't work anymore for me in Visual Studio 2015 (or maybe it is because the framework version change I made). I fixed it by replacing it by:
System.Web.Hosting.HostingEnvironment.SiteName

Do you have access to the Request object at all? If so i think you could use
Request.Url.Authority
It will return the dns host name which is what you are looking for.
G

Have you tried: -
System.Web.HttpContext.Current.Request.ServerVariables["SERVER_NAME"]
Thanks,
Phil.

Related

How to get rid of YSOD but keep the standard logging functionality?

The default options of <system.web><customErrors> don't work very well in an ASP.NET MVC app if you want to return the actual HTTP status codes like 404, 500 etc. instead of 302 (because it works only with Web Forms).
No matter where and how I catch exceptions (like in Global.asax or in a global error handler) and "swallow" the exception (e. g. using Server.ClearError()), I have full control of what is rendered to the client but I loose the default logging (Web Events written to the Windows Event Log). This kind of logging works fine for me, it's very robust and I have Event Log monitoring anyway, so that's exactly what I want for logging.
Is there anything I can do to e. g. in Global.asax to simply force ASP.NET to log the current error as a Web Event and then clear the error?
A solution that requires ASP.NET 4 and IIS7.5 Integrated Mode would be fine.
One option that kind of works would be the use of <system.webServer><httpErrors>, however this works only with existingResponse="Replace", not Auto, because ASP.NET seems to automatically set TrySkipIisCustomErrors=true, and I would need this on Auto since a few controllers of my application need to return custom error pages (XML).
Thank you very much in advance!

Obscure IIS7 ISAPI Filter problem

I am currently trying to migrate a legacy ASP application from Windows Server 2000 and IIS5 on to Windows Server 2008 and IIS7. The authentication for this application uses an ISAPI filter. Both the application and the ISAPI filter work in the new environment except for one problem. The application in question is set up to be the root application (as in you access it by going to http://hostname/application.asp). If I access it by the stated URL, it asks for my authentication details, the ISAPI filter gets passed the correct login details and then does its thing. Then I added a default document to IIS7 to point to application.asp. Now if I access the application by going to http://hostname, the ISAPI filter gets passed the wrong login details. The ISAPI filter implements this method:
DWORD CMyISAPIFilter::OnAuthentication(CHttpFilterContext* pCtxt, PHTTP_FILTER_AUTHENT pAuthent)
In it, the pAuthent object gets passed in by IIS when it calls the ISAPI filter and has the pszUser property which normally holds the value entered in the authentication dialog. In my case (when accessing the website by using the default document entry), the pszUser property holds the value of the Windows account that is running the application pool instead. Just to make things clear, the ISAPI filter gets passed the correct value if I type in the application.asp part.
I'm quite stumped at this problem and have no clue as to how to solve it. To make matters workse I can't really rebuild/modify the ISAPI filter and am quite the beginner when it comes to IIS administration.
I have also found that authentication works if IIS does not have the Default Document feature installed. Without this feature the default document does not work (naturally) but authentication does. It's only after I add the Default Document feature that authentication breaks.
Thanks for your help!
Sounds like the web site is configured to use integrated security on the root level. Make sure you remove the checkmark from the Integrated Windows Authentication checkbox in the Directory Security-Tab.
The Node "Web Sites" can be configured as well. So make sure you don't miss that one ;-)
EDIT: Damn i can't upload a picture. Would make it easier for you to find the setting.

WCF WebInvoke, Asp.net script using OPTIONS instead of POST Method

We have a WCF service in our web application which is being called by browser through ajax(jquery and asp.net scriptmanager)
In this service some of the methods are WebGet and some of them are WebInvoke
Problem is now any request to WebInvoke method through asp.net scriptmanager is making request using http 'OPTIONS' instead of 'POST'. And webserver is going to deny this request as it don't allow OPTIONS
POST should be default for WebInvoke attribute when no method is defined.
We started having this problem since last couple of weeks just spotted today. We upgraded everything to .net 4.0 around same time so not sure .net 4.0 has something changed.
Also weird thing is everything works fine when using website like xyz.com we only see problem when using www.xyz.com
Any idea what it could be?
Edit: Ok got little closure
Problem is Asp.net scriptmanager does not allow cross site reference in ajax.
But wait we do not do anything that calls cross site.
What i found is when i use www.a.com some how js files generated by script manager wants to use a.com not www.a.com.
Any idea how to resolve this?

ASP.Net when post back get The specified URL cannot be found

When clicking the save button on a asp.net web form page, I get the following immediately:
The specified URL cannot be found
This does not happen when I try this using a browser on the web server.
Has anyone run across this problem before? Is this some kind of security issue?
More information. I tried a test page that included all of the form fields and a button that didn't do anything but write to a log4net log. Same problem on the button click. This is just weird.
Found the solution to this!!! Turns out the firewall was blocking request that had more then 40 parameters in the query string.
Could you provide a bit more information?
This could be caused by a lot of things:
Is IIS configured properly for the site? Authentication, host header settings?
Did you try http://localhost/.. on the webserver or did you use the webserver's host name?
Are you redirecting the user? What are you doing in the button event?
Are you using URL routing? Or a handler that might be causing problems?
What version of the .NET framework are you running? 1.1 / 2.0 / higher?
Does the page include javascript that could cause unexpected results?
Checked for crazy stuff in your web.config?
Sometimes it's a good idea to take a step back and create a new ASPX page and test if that one works. Start from a predictable scenario and move towards your current scenario in baby steps.

ASP.Net MVC Controller causing Not Found error when deployed to IIS 7

I am developing a website using ASP.Net MVC. I have a method in the Home controller which returns a partial view when called. The issue is when I am calling the controller method using jQuery, I get a 404 Not Found error.
This only happens when the application is deployed on IIS 7. It works fine during development, but after deployement, none of the controller are found when called through jQuery.get method or post.
Thanking you all in advance for helping me.
I had the same problem. The simple fix for me was to do a aspnet_regiis -r
is the error an asp.net error (yellow and red) or the default iis7 html error? if its iis7 default error, make sure IIS7 has asp.net installed, that caught me out when i first started working with it. (programs and features and add windows components and make sure asp.net is selected under IIS)
Actually, the website is running, I can navigate among pages, only if there is a call through jQuery.get or post methods, then the error occurs. I guess, there is an issue with ISS 7 regarding recognizing that its a controller method. The error text is given below.
HTTP Error 404.0 - Not Found
The resource you are looking for has been removed, had its name changed, or is temporarily unavailable .
Thanks.
Make sure you have ASP.NET MVC installed and running. Also, make sure that your application is installed properly with appropriate references.
Like Tony says above, make sure you can actually access the complete URI (controller/method) in a browser to see if it works ok.
what is the URL that the jQuery.get method is accessing?
If it does, check how you have wired your Ajax call.
Do the method in your code have the AcceptVerbs attribute defined? [AcceptVerbs(HttpVerbs.Post)]
If you are deploying your mvc app as a virtual web make sure that BOTH your virtual web and the root site has its Application Pool set as Integrated Pipeline.
If you are running ASP.NET MVC on IIS7, make sure you configure IIS to run in "Integrated Pipeline Mode" as opposed to "Classic". You'll have more success with routing in this mode. More here.
I had same problem.
solved issue by changing the ajax url.
Just for an example ,
if you are using ajax call to the controller:
have to write url as like this == url: '#Url.Action("actionmethod", "controller")',
instead of ===== url: '/controller/actionmethod',
Thanks

Resources