ASP.NET Routing Constraint For Specific URL
I have an asp.net application where I need to have a general route for given Medical Specialties to route to a given page except for one which I want to route to a different page.
For example I want /Specialties/Allergy, /Specialties/Nutrition to all route to a given page like they currently do. The part I can't figure out is I want the URL to still show /Specialties/Urology but have urology rout to a different page because it is becoming essentially it own subsite where the other ones are not.
I'm sure I need to add a Route constraint but I can't seem to figure out how to accomplish that.
I have include all of the relevant code I currently use for the general route.
private static void AddEntityRoute(string name, string url, string path, string dir)
{
((List<string>)HttpContext.Current.Application["RouteList"]).Add(url);
RouteTable.Routes.Add(name,
new Route(url, new EntityRouteHandler(path, dir)));
}
public class EntityRouteHandler : IRouteHandler
{
string _virtualPath;
private readonly string baseDir;
public EntityRouteHandler(string virtualPath, string baseDir)
{
_virtualPath = virtualPath;
this.baseDir = baseDir;
}
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
var display = BuildManager.CreateInstance开发者_如何转开发FromVirtualPath(
_virtualPath, typeof (Page)) as IEntityDisplay;
display.FriendlyURL = baseDir + requestContext.RouteData.Values["name"];
return display;
}
}
AddEntityRoute("Specialties", "Specialties/{name}", "~/SpecialtyDisplay.aspx", "/Specialties/");
You're approaching this the wrong way.
If your intention is to direct a specific url to a different controller/action, then just add another route.
routes.MapRoute("", "Specialties/Urology", new { controller = "someothercontroller", action = "someotheraction" });
routes.MapRoute("", "Specialties/{speciality}", new { controller = "specialities", action = "show" });
Here a request to "Specialities/Urology" would match the first route. A request to "Specialities/Allergy" would match the second.
精彩评论