How to get the url with application name in .net mvc? - c#

Is there any way that I get the host address with the Application Name in IIS of my websites? To make my question clear here are few examples of what I want:
http://example.com/constructionSite/myController/myAction?blah=blahh
http://example.com/tr.Implementation/myOtherController/myAction?blah=blahh
http://example.com/anotherApplication/myController/myAction?blah=blahh
http://example.com/myOtherOtherController/myAction?blah=blahh
In my IIS I have applications like those above constructionSite, tr.Implementation, anotherApplication, myController. I need to get the urls with the application names on IIS, in other words up until the myController/...... And myController will not allways be the same controller, so I cannot use .IndexOf() method.
I tried playing with System.Web.HttpContext.Current.Request.Url.AbsoluteUri, System.Web.HttpContext.Current.Request.Url.AbsolutePath these paths, but it doesn't satisfy me.
What should be the appropriate approach to get the url with application name?

you can try using Url.Content("~")
#string.Format("{0}://{1}{2}", Request.Url.Scheme, Request.Url.Authority, Url.Content("~"))

Related

ASP .Net MVC 5 redirect issue

Currently I have an internal server serv that has an application app.
It is running MVC 5 and has 2 pages Account/Logon which maps to controller and an action.
And Orders/Display (again controller/action)
In order for internet users in the outside world to see I have a host server host running IIS. It maps host URLs to the internal app like so
host/Account/Logon -->serv/app/Account/Logon
So basically anything after host/... gets changed to serv/app/....
My problem is if I do RedirectToAction("Display","Orders");
(there are lots of these redirects because there's lots of controller/action combos.
The internal server grabs the host portion but also tacks on "app" in order
to switch to the next page or route
So,host/app/Orders/Display
My problem is that the host IIS maps this URL to host/app/app/Orders/Display
because that what it's mapping is set to do. always add "app"
How can I solve this issue? Is there any way for serv do
a relative mapping? In order words it only sees the original host/Account/Logon
so on switch it would be host/Orders/Display?
Use Redirect() instead of RedirectToAction() and pass in the url.

Getting IIS Websites' Domain names

I've created a web application to list all websites on IIS with their names, ports and physical paths. it's also supposed to get the IIS websites domain names. the application is installed on IIS and all functions work great except for returning domain names.
I wrote the below code and it returned "localhost:8183" for all websites. 8183 is the application's port itself.
Request.Url.Scheme + "://" + Request.Url.Authority +
Request.ApplicationPath.TrimEnd('/') + "/";here
then I tried this one and it just returned "localhost" instead of domain names.
HttpContext.Current.Request.Url.Host;
I'm so curious to know where I'm going wrong. please let me know if any further information is needed.
A web server does not actually know what domains it can serve. It knows what domain was in the request headers in the thread for that request, but you can set up any domain you like to point to any server's IP address and the server will accept it.
If you have set up bindings by domain (using Server Name Indication), you can get that information from the objects in Microsoft.Web.Administration ( for each site in ServerManager.Sites, for each Binding in site.bindings, if Binding.Protocol = http or https and Binding.Host !="" then write out Binding.Host) but that won't tell you about any domains that aren't in bindings.
If you use request object it will only give details about the website where your code is running. You need to use Directory Services to connect to IIS and do you work.
[From already answered question]- Here's an article explaining how this could be done using classes from the System.DirectoryServices namespace as well as the Microsoft.Web.Administration namespace which was introduced with IIS 7.
You can use below code to get the Domain name of current server:
System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().DomainName
or you can use below code to get current user's domain:
System.Security.Principal.WindowsIdentity.GetCurrent().User. (string value)
Then you can trim it.
Happy coding.

In C# MVC4 how to get the server path?

In c# MVC4 how to I get the server path. for example: http://192.169.1.120:60632
Is there a helper function that I can convert something like ~/aFolder/file.htm into an absolute path? Ideally I would like a way of taking any given url and convert into a full absolute url. E.g. can cope with..
/aFolder/file.html -> http://192.169.1.120:60632/aFolder/file.html
http://website.com/file.html -> http://website.com/file.html
And will work anywhere within the c# code - i.e. in a action controller, signalR hub, model etc.
And it will still work when I deploy to a remote server.
Use the Request.Url property available in Controller. This returns a Uri object containing info on the request. From there, you can access the AbsoluteUri and Port properties to get the info that you need.
If you are interested in getting the url info from SignalR, try looking at this question and answer.
Try this one,
System.Web.HttpContext.Current.Server.MapPath(#"~/FolderName")
You can try these as well
string path = AppDomain.CurrentDomain.GetData("FolderName").ToString();
HostingEnvironment.MapPath(#"~/FoldeName");
In MVC, you can get a fully qualified URL using the fourth parameter of Url.Action - protocol:
Url.Action("Index", "Home", null, Request.Url.Scheme)

Wrong host returned using HttpContext.Current.Request.Url.Host

We have multiple domains for one of our websites.
e.g. mydomain-uk.com and mydomain.co.uk
I have a handler which creates an XML sitemap and it uses HttpContext.Current.Request.Url.Host to retrieve the host site.
When my browser is on mydomain.co.uk/handler it retrieves mydomain-uk.com as the host
How can I ensure it always retrieves mydomain.co.uk ?
Is there a preference order configured somewhere on the server?
The host is get it from the URL on the request, and this is logical, you can not change this.
To solve this, create a static variable with your URL name, even better place it on your web.config, and just get this variable and not the Url.Host
Hope this help
Don't point all of your domains at the website. Have the extra domains perform a 301 redirect to the main domain name. This will also help resolve confusion by search engines when they try to resolve your site as to which site is the original source of your content, and will prevent inbound links from other websites from using a mixture of domains which will only exacerbate the problem.
Don't forget that HttpContext.Current.Request.Url.Host is simply going to return whatever HOST was requested at the time it happened. If the client requested something else, HttpContext.Current.Request will reflect this.

Wildcard mapping for ASP.NET and issues with PHP

I have an application written in .NET 3.5 with C# as the language. I'm using Web Forms, but using url routing with the routes defined in my global file. Everything is working as expected. In order for the pretty paths (see: user/665 instead of user.aspx?uid=665) to work properly, I had to add a wildcard mapping in IIS5.1 (local box, not test, staging, or production) the aspnet_isapi file for the 2.0 framework. Everything works fine.
Now, my site needs a plugin for PHP. However, the PHP files are now being serviced by ASP.NET due to the wild card mapping, and hence are not processed by the PHP interpretter. Is there any way to get around this? Would I have to add some sort of handler to my web app that will take all PHP requests being handled by the ASP.NET framework and have them routed to the PHP engine? Is there an easier way? Maybe a way to exclude them in the web.config (PHP files) and have them served by the proper PHP engine?
Thanks all!
-Steve
This is a solution, but is not an elegant way (IMHO):
Create a virtual directory
Have it point to the folder with the files (in this case, a PHP plugin)
Give it the proper permissions
Change the config options for the virtual directory in IIS and make sure the wildcard mapping for that directory is removed.
This works like a charm for my situation. However, is there any way to not have to deal with virtual directories?
The problem is that the PHP extension needs to be registered.
In IIS Manager right-click on Default Website -> Properties -> Home Directory -> Configuration
Under Application Mappings make sure that .php is added and is it pointing to PHP.EXE. There should be an entry like this: extension .php, executable path C:\PHP\PHP.EXE %s %s
From what I gather, the problem is that ASP.NET is attempting to route your PHP requests, so what I would do is add a StopRoutingHandler() to your routes in the global.asax. Something like this should work:
routes.Add(new Route("{resource}.php/{*pathInfo}", new StopRoutingHandler()));
Edit: Be mindful that routes are processed in order, so I would add this to the top of your routes.

Categories

Resources