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

2019-02-16 04:22发布

问题:

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() { /* ... */ }
}