MVC 4包罗万象的路线从未达到过 [英] MVC 4 catch all route never reached

查看:161
本文介绍了MVC 4包罗万象的路线从未达到过的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当试图建立一个包罗万象的MVC 4所有航线(这是我发现的几个例子,并根据我的code)它返回一个404错误。我在IIS 7.5上运行此。这似乎是一个简单的解决方案,所以我缺少什么?

一个说明,如果我移动默认的路线上面的包罗万象的路线它的作品。但当然再没有其它控制器都曾经达到。

下面是code:

Route.Config:

  routes.MapRoute(
            名称:默认,
            网址:{控制器} / {行动} / {ID}
            默认:新{控制器=家,行动=索引,ID = UrlParameter.Optional}
        );        routes.MapRoute(
            包罗万象,
            {*} dynamicRoute
            新{控制器=包罗万象,行动=ChoosePage}
        );

控制器:

 公共类CatchAllController:控制器
{    公众的ActionResult ChoosePage(字符串dynamicRoute)
    {
        ViewBag.Path = dynamicRoute;
        返回查看();
    }}


解决方案

由于创建包罗万象路线的最终目标是要能够处理动态URL和我无法找到上述直接回答原来的问题,我从不同的角度来处理我的研究。在这方面,我碰到这个博客帖子就来了:自定义404时,没有路由匹配

该解决方案允许一个给定的URL中的多个部分的处理
(即www.mysite.com/this/is/a/dynamic/route)

下面是最后的定制控制器code:

 公众覆盖一个IController CreateController(System.Web.Routing.RequestContext的RequestContext,串controllerName)
 {
     如果(RequestContext的== NULL)
     {
         抛出新的ArgumentNullException(RequestContext的);
     }     如果(String.IsNullOrEmpty(controllerName))
     {
         抛出新的ArgumentException(MissingControllerName);
     }     VAR controllerType = GetControllerType(RequestContext的,controllerName);     //这是通常会返回一个404
     //与路线包罗万象控制器替换
     如果(controllerType == NULL)
     {
        //建立与各阶层的动态路径变量
        VAR dynamicRoute =的string.join(/,requestContext.RouteData.Values​​.Values​​);        //路线全部接收控制器
        controllerName =包罗万象;
        controllerType = GetControllerType(RequestContext的,controllerName);
        requestContext.RouteData.Values​​ [控制器] = controllerName;
        requestContext.RouteData.Values​​ [行动] =ChoosePage;
        requestContext.RouteData.Values​​ [dynamicRoute] = dynamicRoute;
     }     一个IController控制器= GetControllerInstance(RequestContext的,controllerType);
     返回控制器;
 }

When attempting to create a catch all route in MVC 4 (something I've found several examples of, and based my code on) it returns a 404 error. I'm running this on IIS 7.5. This seems like a straight forward solution, so what am I missing?

One note, if I move the "CatchAll" route above the "Default" route it works. But of course then none of the other controllers are ever reached.

Here is the code:

Route.Config:

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

        routes.MapRoute(
            "CatchAll",
            "{*dynamicRoute}",
            new { controller = "CatchAll", action = "ChoosePage" }
        );

Controller:

public class CatchAllController : Controller
{

    public ActionResult ChoosePage(string dynamicRoute)
    {
        ViewBag.Path = dynamicRoute;
        return View();
    }

}

解决方案

Since the ultimate goal of creating the catchall route was to be able to handle dynamic urls and I was unable to find a direct answer to the original issue above, I approached my research from a different perspective. In doing so I came across this blog post: Custom 404 when no route matches

This solution allows handling of multiple sections within a given url (i.e. www.mysite.com/this/is/a/dynamic/route)

Here is the final custom controller code:

public override IController CreateController(System.Web.Routing.RequestContext requestContext, string controllerName)
 {
     if (requestContext == null)
     {
         throw new ArgumentNullException("requestContext");
     }

     if (String.IsNullOrEmpty(controllerName))
     {
         throw new ArgumentException("MissingControllerName");
     }

     var controllerType = GetControllerType(requestContext, controllerName);

     // This is where a 404 is normally returned
     // Replaced with route to catchall controller
     if (controllerType == null)
     {
        // Build the dynamic route variable with all segments
        var dynamicRoute = string.Join("/", requestContext.RouteData.Values.Values);

        // Route to the Catchall controller
        controllerName = "CatchAll";
        controllerType = GetControllerType(requestContext, controllerName);
        requestContext.RouteData.Values["Controller"] = controllerName;
        requestContext.RouteData.Values["action"] = "ChoosePage";
        requestContext.RouteData.Values["dynamicRoute"] = dynamicRoute;
     }

     IController controller = GetControllerInstance(requestContext, controllerType);
     return controller;
 }

这篇关于MVC 4包罗万象的路线从未达到过的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆