开发者

One controller for whole ASP.NET MVC application

I want to have one controller for all site urls eg:

  • www.mysite.com/page1
  • www.mysite.com/another
  • www.mysite.com/third/some-text
  • www.mysite.com/fourth/some/some/some/some

Urls will be created manually, also url can be of "every type"(teoretically you must have posibility to put everything in url) I don't want to put controller name or anything(action name or id) in the url.

How to write url route rule for this ? I am not intersted in disadvantages of such design. This is default ASP.NET MVC routing rule

routes.MapRoute(
    "Default",                                              // Route name
    "{controller}/{action}/{id}",                           // URL with parameters
    new { controller = "Home", action = "Index", 开发者_如何学Cid = UrlParameter.Optional }  // Parameter defaults
);


I won't tell you this is a bad plan. But it is. Anyhow, if you don't need to handle an unlimited url folder depth, the trick is to run things in reverse order:

       //maps virtual redirects
        routes.MapRoute("virtual redirects",
                        "{redirect}",
                        new
                        {
                            area = "",
                            controller = "Content",
                            action = "VirtualRedirect"
                        }
            );

        routes.MapRoute("two level",
                        "{firstPart}/{secondPart}",
                        new
                        {
                            area = "",
                            controller = "Content",
                            action = "PageUrl",
                            thirdPart = ""
                        }
                        );

        //maps general content pages
        routes.MapRoute("page catcher",
                        "{firstPart}/{secondPart}/{thirdPart}",
                        new
                        {
                            area = "",
                            controller = "Content",
                            action = "PageUrl"
                        }
            );

Note you'll want to put any more specific routes above this and you'll still need a default route declared after this for some parts of MVC to work.


I think you can do this way, just override routHandler

        routes.MapRoute(
            "dbroute",
            "{controller}/{action}/{id}",
            new { id = "" }
            ).RouteHandler = new MyRouteHandler();

and you new route handler like this

public class MyRouteHandler : IRouteHandler 
{

    #region IRouteHandler Members

    public IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        IRouteHandler handler = new MvcRouteHandler();
        var vals = requestContext.RouteData.Values;
        if (vals["controller"] != null && 
        (vals["controller"].ToString() == "page1" ||
        vals["controller"].ToString() == "another" ||
        vals["controller"].ToString() == "others" ) )
        {

            vals["controller"] = "Test";// your controller; 
            vals["action"] = "Index";//your action 
        }
        return handler.GetHttpHandler(requestContext);
    }


    #endregion
}


igor,

unless there's a really compelling reason to do so, i'd think of alternative ways to present the urls that you want and think about the discreet functionality 'under the covers' that each logical unit is performing. a similar question was asked a while back which i responded to:

What is the advantage of using multiple Controller classes in ASP.NET MVC?

might give you a few ideas.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜