自定义路由句柄无法正常工作

时间:2014-11-11 09:08:09

标签: asp.net-mvc routes customization handle

阅读本文后:.NET MVC-4 routing with custom slugs

我能够将解决方案应用到我的项目中:

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

并对SlugRouteHandler类进行了一些更改:

public class SlugRouteHandler : MvcRouteHandler
{
    protected override IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        var url = requestContext.HttpContext.Request.Path.TrimStart('/');


        if (!string.IsNullOrEmpty(url))
        {
            if (url.Contains("_escaped_fragment_"))
                requestContext.HttpContext.Response.StatusCode = 401;
            else
                requestContext.HttpContext.Response.StatusCode = 404;

            FillRequest("Error","Index", requestContext);
        }

        return base.GetHttpHandler(requestContext);
    }
    private static void FillRequest(string controller, string action, RequestContext requestContext)
    {
        if (requestContext == null)
        {
            throw new ArgumentNullException("requestContext");
        }

        requestContext.RouteData.Values["controller"] = controller;
        requestContext.RouteData.Values["action"] = action;
    }
}

这是我当地主持人的结果,完美:

enter image description here

但是在将项目发布到主机后,它并没有像我预期的那样工作:

enter image description here

这是否有可能是因为服务器的配置?

0 个答案:

没有答案