Can I use the route restriction here?

If I have the following URL:

/ someurl

And I have two domain names:

us.foo.com

au.foo.com

I want this to be 200 (coincidence):

us.foo.com/someurl

But this is to 404 (does not match):

au.foo.com/someurl

The route is as follows:

RouteTable.Routes.MapRoute( "xyz route", "someurl", new { controller = "X", action = "Y" } ); 

I assume that there are no route values, I can not restrict the host based URL? It is right?

If so, how can I do this, except for the following (ugly) in action:

 if (cantViewThisUrlInThisDomain) return new HttpNotFoundResult(); 

Does anyone have any ideas?

I assume that I am looking for a way to restrict the route through its domain, and not a route marker, if that makes sense.

+4
source share
1 answer

You can write your own route:

  public class MyRoute : Route { public MyRoute(string url, object defaults) : base(url, new RouteValueDictionary(defaults), new MvcRouteHandler()) { } public override RouteData GetRouteData(HttpContextBase httpContext) { var url = httpContext.Request.Url; if (!IsAllowedUrl(url)) { return null; } return base.GetRouteData(httpContext); } private bool IsAllowedUrl(Uri url) { // TODO: parse the url and decide whether you should allow // it or not throw new NotImplementedException(); } } 

and then register it in the RegisterRoutes method in Global.asax :

 routes.Add( "xyz route", new MyRoute( "{someurl}", new { controller = "Home", action = "Index" } ) ); 
+7
source

Source: https://habr.com/ru/post/1369436/


All Articles