开发者

ASP.NET MVC3 Route Mapping reduction help

I am trying to play with with is possible with routes in my ASP.NET MVC3 application and try reduce some of my mapping code. I am using trying to us a common UserController/View accross my application across a number of different entities. For example, you have Stores and Companies, and each has their own set of users. Is there any way to reduce the following two routes:

        routes.MapRoute(
            "StoreUsers", // Route name 
            "Store/Details/{entityID}/User/Index", // URL with parameters
            new { controller = "User", action = "StoreIndex"} // Parameter defaults
        );

        routes.MapRoute(
            "CompanyUsers", // Route name 
            "Company/Details/{entityID}/User/Index", // URL with parameters
            new { controller = "User", action = "CompanyIndex"} // Parameter defaults
        );

To something which resembles this?

      开发者_高级运维  routes.MapRoute(
            "EntityUsers", // Route name 
            "{entity}/Details/{entityID}/User/Index", // URL with parameters
            new { controller = "User", action = entity + "Index"} // Parameter defaults
            new { entity = "(Store|Company)" } //Parameter constraints
        );

and have the {action} parameter (and {action} default) set to: {entity} + "Index" so it can be used for entity entity which matches the constraints.

I am only reducing 2 routes to 1 here, but my real issue involves more then just these two entities, and if I can get this to work, I can use this for other controllers that have to mimic the same functionality and other actions as well (Create, Edit, etc).

Thanks


I figured the answer had to be out there and I was just not searching for the right things, i scoured StackOverflow for a bit and was able to find this question which helped me develop a solution:

asp.net mvc complex routing for tree path

I could set up a route to look like this:

        routes.MapRoute(
            "EntityUsers", // Route name 
            "{entity}/Details/{entityID}/{controller}/{subaction}/{id}", // URL with parameters
            new {controller = "User", subaction = "Index", id = UrlParameter.Optional}, // Parameter defaults
            new {entity = "(Lender|Dealer)", controller="User"}
        ).RouteHandler = new UserRouteHandler();

and the UserRouteHandler class looks as follows:

public class UserRouteHandler : IRouteHandler {
    public IHttpHandler GetHttpHandler(RequestContext requestContext) {
        string entity = requestContext.RouteData.Values["entity"] as string;
        string subaction = requestContext.RouteData.Values["subaction"] as string;
        if (entity != null && subaction != null)
        {
            requestContext.RouteData.Values["action"] = entity + subaction;
        }
        return new MvcHandler(requestContext);
    }
}

In the end, I was way over complicating the issue and don't need this, but its good to know you can have this flexibility

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜