【问题标题】:Asp.net mvc weired behaviour in route data binding路由数据绑定中的 Asp.net mvc 奇怪的行为
【发布时间】:2013-11-17 21:17:55
【问题描述】:

大师,

我遇到了一个非常简单的路由数据绑定问题。

请参考以下代码。

公共类 RemoveDashRouteHandler : MvcRouteHandler

{

    protected override IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        requestContext.RouteData.Values["controller"] = ((string)requestContext.RouteData.Values["controller"]).Replace("-", String.Empty);

        return base.GetHttpHandler(requestContext);
    }
}

这里是我注册的路线

routes.MapRoute (

              name: "Categorywise",
              url: "{action}-{controller}/{nm}/{p}"
              ).RouteHandler = new RemoveDashRouteHandler();

然后我做了一个这样的简单模型

公共类参数

{
    public string nm { get; set; }
    public string p { get; set; }
}

现在的问题是,当我点击 URL 时:http://{mylocalip}:52730/Authority-cars/1/1 我总是把所有参数都设为空

用下面的方法重写同样效果很好。

public ActionResult Authority(string nm, string p)

    {
        ViewBag.ActionMethod = "Autority";

        return View("~/Views/Cars/Index.cshtml", new Params { nm = nm, p = p });
    }

我做错了什么。 请帮忙。

提前致谢

【问题讨论】:

    标签: asp.net-mvc-4 asp.net-mvc-routing


    【解决方案1】:

    根据您定义路由的方式,无需编写自定义路由处理程序来考虑路由中的破折号。

    routes.MapRoute(
                    "Dash",                                              
                    "{action}-{controller}/{nm}/{p}",                       
                    new { controller = "Home", action = "Index" }
                );
    

    【讨论】:

    • 是的,您在这种情况下是正确的,但在我的应用程序中,很少有路由与控制器用连字符分隔。奇怪的是,我无法获取路线值。
    猜你喜欢
    • 2011-03-31
    • 1970-01-01
    • 2016-11-28
    • 2017-09-01
    • 2016-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-14
    相关资源
    最近更新 更多