ASP.NET MVC自定义路由类 - 无法路由数据 [英] ASP.NET MVC Custom Route class - Can't get route data

查看:103
本文介绍了ASP.NET MVC自定义路由类 - 无法路由数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现自己的路由类,从默认的路径继承。

I'm trying to implement my own route class, inheriting from the default Route.

这是我的自定义路由类是这样的:

This is what my custom route class looks like:

public class FriendlyRoute : Route
{
    public FriendlyRoute(string url, RouteValueDictionary defaults, IRouteHandler routeHandler)
        : base(url, defaults, routeHandler)
    {

    }

    public override RouteData GetRouteData(HttpContextBase httpContext)
    {
        var routeData = base.GetRouteData(httpContext);

        var controllerName = routeData.Values["controller"].ToString();
        var actionName = routeData.Values["action"].ToString();

        routeData.Values["controller"] = fix(controllerName);
        routeData.Values["action"] = fix(actionName);

        return routeData;
    }

    private string fix(string name)
    {
        //Remove dashes: "my-controller" => "mycontroller"
    }
}

我在做什么正在接受URL以破折号和路由到正确的动作(我的控制器/我的行动,以myController的/ MyAction),但我对这个自定义路由一些更多的计划。

What I'm doing is accepting urls with dashes and routing the to the correct action ("my-controller/my-action" to "MyController/MyAction"), but I have some more plans for this custom route to.

把我的自定义路由类的动作,我用下面的路由配置:

To put my custom route class in action, I use the following route config:

routes.Add("Default",
           new FriendlyRoute("{controller}/{action}/{id}",
                             new RouteValueDictionary(new { controller = "Public", action = "Start", id = UrlParameter.Optional }),
                             new MvcRouteHandler()));

这工作得很好!但我不愉快的URL结构。我希望有一些网址,不带控制器名称只是动作的名字(例如/约,/接触)和一些与控制器名称(如/我的空间,/我的空间/发票)。我使用默认路由类(不是我自己的自定义)开始,解决这个问题:

This works fine! But I'm not happy with the url structure. I want to have some urls with no controller names only action names (e.g. "/about", "/contact") and some with controller names (e.g. "/mypage", "/mypage/invoices"). I start by using the default route class (not my own custom) and fix this problem:

routes.Add("MyPages",
           new Route("MyPage/{action}",
                     new RouteValueDictionary(new { controller = "MyPage", action = "Summary"}),
                     new MvcRouteHandler()));

routes.Add("Public",
           new Route("{action}/{id}",
                     new RouteValueDictionary(new { controller = "Public", action = "Start", id = UrlParameter.Optional }),
                     new MvcRouteHandler()));

这也工作正常,但现在有与破折号的网址不支持。所以,我只要将我的自定义路由类到路径配置:

This also works fine, but now there's no support for urls with dashes. So I just swap in my custom route class into the route config:

routes.Add("MyPages",
           new FriendlyRoute("MyPage/{action}",
                             new RouteValueDictionary(new { controller = "MyPage", action = "Summary" }),
                             new MvcRouteHandler()));

routes.Add("Public",
           new FriendlyRoute("{action}/{id}",
                             new RouteValueDictionary(new { controller = "Public", action = "Start", id = UrlParameter.Optional }),
                             new MvcRouteHandler()));

现在,当我跑我尝试去到默认页面(/)崩溃,因为调用base.GetRouteData(HttpContext的)在我FriendlyRoute.GetRouteData()返回null应用程序。

Now when I run the application I try to go to the default page ("/") it crashes because the call to base.GetRouteData(httpContext) in my FriendlyRoute.GetRouteData() returns null.

我都新创建一个自定义路由类,所以我在做什么错将AP preciated任何提示。

I'm all new to creating a custom route class, so any hints on what I' doing wrong would be appreciated.

推荐答案

添加额外的两条路线,当根URL被击中后(/),它将对顺序路由定义从上到下的顺序处理,直到比赛制作。所以,现在是第路线我的页面/ {行动}它会调用公共抽象的RouteData GetRouteData(HttpContextBase HttpContext的); 方法,而这又与路由定义的URL匹配和检查约束。它在比赛和约束检查的情况下,,否则返回 RouteValueDictionary 对象。所以对于第一路由定义势必返回null作为URL不匹配。你应该添加一个空检查作为foollowing

After adding the additional two routes, when the root url is hit ("/") it will be processed against route definitions sequentially top to bottom until a match is made. So now for the first route "MyPage/{action}" it will call the public abstract RouteData GetRouteData(HttpContextBase httpContext); method, which in turn matches the url with the route definition and check for constraints. It returns a RouteValueDictionary object in case of match and constraint check, null otherwise. So for the first route definition it is bound to return null as the url does not match. You should add a null check as foollowing

if (routeData != null)
{
    var controllerName = routeData.Values["controller"].ToString();
    var actionName = routeData.Values["action"].ToString();

    routeData.Values["controller"] = Fix(controllerName);
    routeData.Values["action"] = Fix(actionName);
}

相关资源:<一href=\"http://www.dotnetframework.org/default.aspx/4@0/4@0/untmp/DEVDIV_TFS/Dev10/Releases/RTMRel/ndp/fx/src/xsp/System/Web/Routing/Route@cs/1305376/Route@cs\"相对=nofollow> Route.cs

希望这有助于。

这篇关于ASP.NET MVC自定义路由类 - 无法路由数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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