【问题标题】:How to fall back from a custom MvcRouteHandler to next one in Route Table如何从自定义 MvcRouteHandler 回退到路由表中的下一个
【发布时间】:2014-12-24 13:46:14
【问题描述】:

我有一个自定义的 MvcRouteHandler,它检查数据库是否存在 Url,并将其与一些控制器操作和 Id 配对。

但是,如果此路由处理程序在数据库中找不到匹配的对,我希望 MVC 继续尝试在路由表中使用其他已定义的路由处理程序。

我该怎么做?

更新:(添加示例代码)

routes.MapRoute(
    name: "FriendlyRoute",
    url: "{FriendlyUrl}").RouteHandler = new FriendlyRouteHandler();

FriendlyRouteHandler 是:

public class FriendlyRouteHandler : MvcRouteHandler
{
    private TancanDbContext db = new MyDbContext();

    protected override IHttpHandler GetHttpHandler(System.Web.Routing.RequestContext requestContext)
    {
        if (requestContext.RouteData.Values["FriendlyUrl"] != null)
        { 
            string friendlyUrl = requestContext.RouteData.Values["FriendlyUrl"].ToString();

            //Here, you would look up the URL Record in your database, then assign the values to Route Data
            //using "where urlRecord.Url == friendlyUrl"         
            try
            {
                UrlRecord urlRecord = db.UrlRecords.Single(u => u.URL == friendlyUrl);          
                //Now, we can assign the values to routeData
                if (urlRecord != null)
                {
                    requestContext.RouteData.Values["controller"] = urlRecord.Controller;
                    requestContext.RouteData.Values["action"] = urlRecord.Action;
                    if(urlRecord.EntityId != null)
                    requestContext.RouteData.Values["id"] = urlRecord.ObjectId;
                   }
                }
                else
                {
                    //Here, I want to redirect to next RouteHandler in route Table
                  requestContext.RouteData.Values["controller"] = friendlyUrl;
                }
            }
            catch (Exception ex)
            {               
                //throw;
                //Here too, I want to redirect to next RouteHandler in route Table
                requestContext.RouteData.Values["controller"] = friendlyUrl;
            }
        }
        return base.GetHttpHandler(requestContext);
    }
}

添加此行后,它似乎可以工作:

requestContext.RouteData.Values["controller"] = friendlyUrl;

我是幸运还是这是正确的做法?我需要在某处使用 IRouteConstraint 吗?

顺便说一句,我的影响力是this article by Adam Riddick

【问题讨论】:

  • 请先显示一些代码。
  • 已添加代码,谢谢@abatishchev

标签: c# asp.net-mvc asp.net-mvc-routing mvcroutehandler


【解决方案1】:

您想为此使用自定义约束,而不是自定义处理程序。

routes.MapRoute(
    name: "example",
    url: "{friendly}",
    defaults: new { controller = "FriendlyController", action = "Display" },
    constraints: new { friendly = new FriendlyUrlConstraint() }
);

然后约束变为:

public class FriendlyUrlConstraint : IRouteConstraint
{
    public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
    {
        var friendlyUrl = values[parameterName];

        // Your logic to return true/false here
    }
}

【讨论】:

  • 感谢您的回答,但我已经有一年多没有接触 ASP .Net 了。
  • 是的,我认为这对您没有帮助,但是下一个在搜索其他内容时偶然发现您的问题的人(就像我所做的那样)可能会发现它很有用 :) 干杯
猜你喜欢
  • 2010-09-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-02
  • 2020-04-19
  • 1970-01-01
相关资源
最近更新 更多