ASP.NET MVC generate subdomain URL in View

How do I generate a subdomain URL in the view? For ex, My application is on www.lol.com, which could change sometime in the near future. I want to link to email on google apps located on the subdomain mail.lol.com.

How do I accomplish this so I dont have to hardcode an absolute URL?

Answers


ASP.NET MVC isn't going to handle this for you.

Luckily, you're not the first one to want to do something like this. Since you have full control over routing in .NET MVC, you just have to customize something for yourself.

If you want something simple that will take out the www and replace it with mail (or append mail if there is no www), then you can check out the Request.Headers["host"] value and modify it as necessary.

If you actually want something flexible to handle the routing for you then you could check out this post on how to get started:

Hanssens.org | ASP.NET MVC Subdomain Routing


You can check Request.Headers["Host"] to find the current hostname.

You may want to strip out the www., if present.


Need Your Help

Android: WindowManagerGlobal.closeAll() ArrayIndexOutOfBoundsException

android android-view android-windowmanager

While the Android app is working normally on most devices, for some people this error occurs on Galaxy S4: