How can we make an ASP.NET MVC4 route based on a subdomain?

时光怂恿深爱的人放手 提交于 2019-11-30 22:01:25

问题


How can we make an ASP.NET MVC4 route that uses subdomain information to determine its route? For example:

website1.domain.com goes to domain.com\websites\1

website2.domain.com goes to domain.com\websites\2

This is a dynamic mapping like this: websiteN.domain.com goes to domain.com\websites\N

I have a username parameter,How Can I pass through controller/action?


回答1:


ASP.NET's built-in routing doesn't directly support sub-domain routing. But fortunately, there's AttributeRouting, which is a very popular add-on library for routing that allows you to do lots of fancy routing, including sub-domain routing.

Here's an example from the Attribute Routing site:

[RouteArea("Users", Subdomain = "users")]
public class SubdomainController : Controller
{
    [GET("")]
    public ActionResult Index() { /* ... */ }
}


来源:https://stackoverflow.com/questions/14632057/how-can-we-make-an-asp-net-mvc4-route-based-on-a-subdomain

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!