【问题标题】:asp.net mvc routing id parameterasp.net mvc 路由id参数
【发布时间】:2010-04-30 08:30:54
【问题描述】:

我正在使用 asp.net mvc 开发一个网站。我有路线

routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    // Parameter defaults
);

这是默认路由。我有一个方法

public ActionResult ErrorPage(int errorno)
{
    return View();
}

现在如果我想用http://something/mycontroller/Errorpage/1 运行这段代码 它不起作用。但是如果我将参数名称从errorno 更改为id 有用。

此方法是否必须具有相同的参数名称?还是我需要为这种情况创建单独的路线?

【问题讨论】:

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


【解决方案1】:

因此,您有一个名为errorno 的参数,并且您希望它具有来自参数id 的值。这显然是绑定问题。

如何解决:

  1. 为模型绑定器创建一个类:

    public class ParameterBinder : IModelBinder
    {
        public string ActualParameter { get; private set; }
    
        public ParameterBinder(string actualParameter)
        {
            this.ActualParameter = actualParameter;
        }
    
        public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            object id = controllerContext.RouteData.Values[this.ActualParameter];
            return id;
        }
    }
    
  2. 为自定义模型绑定创建自定义属性:

    [AttributeUsage(AttributeTargets.Parameter)]
    public class BindParameterAttribute : CustomModelBinderAttribute
    {
        public string ActualParameter { get; private set; }
    
        public BindParameterAttribute(string actualParameter)
        {
            this.ActualParameter = actualParameter;
        }
    
        public override IModelBinder GetBinder()
        {
            return new ParameterBinder(this.ActualParameter);
        }
    }
    
  3. 根据需要将新属性应用于您的操作参数:

    public ActionResult ErrorPage(
    [BindParameter("id")]
    int errorno)
    {
        return View();
    }
    

现在您的errorno 将具有值,该值作为id 传递给您的网址。

注意:您可以从上面的示例中删除参数id,如果您确定只需要为id 解决它。

离开这种方式也可以绑定其他参数。

【讨论】:

    【解决方案2】:

    选项 1

    routes.MapRoute(
        "Default",
        "{controller}/{action}/{id}",
        new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
    
    public ActionResult ErrorPage(int id)
    {
        return View();
    }
    

    选项 2

    routes.MapRoute(
        "Default",
        "{controller}/{action}/{errorno}",
        new { controller = "Home", action = "Index", errorno = UrlParameter.Optional }
    );
    
    public ActionResult ErrorPage(int errorno)
    {
        return View();
    }
    

    选项 3

    routes.MapRoute(
        "Default",
        "{controller}/{action}/{id}",
        new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
    
    public ActionResult ErrorPage(int id)
    {
        int errorno = id;
        return View();
    }
    

    【讨论】:

      【解决方案3】:

      使用绑定属性前缀:

      public ActionResult Customer([Bind(Prefix = "id")]string cname) {}
      

      【讨论】:

        【解决方案4】:

        @Parminder 默认路由可以使用一个参数“id”处理所有操作。而且我认为并不是每个动作都需要这个参数。所以我改变了我的默认路线

        routes.MapRoute(
            "Default", 
            "{controller}/{action}", 
            new { controller = "Home", action = "Index"} 
        );
        

        您可以添加一条新路线:

        routes.MapRoute("errorpage", "yourcontroller/errorpage/{errorno}",
            new {controller="controllername", action="errorpage"});
        

        这只是处理你的控件名称是“controllername”。如果要处理所有控制器,可以添加:

        routes.MapRoute("errorpage", "{controller}/errorpage/{errorno}",
            new {controller="controllername", action="errorpage"});
        

        如果您需要大量自定义路由,此方法将在 global.asax 中创建大量代码。

        【讨论】:

          【解决方案5】:

          您可以在默认根目录中重命名参数(这可能不是一个好主意)或在操作方法中重命名它。添加另一个根将无济于事,因为它将与默认根相同,并且给定一个 url,路由引擎无法区分两者,并且始终采用列表中的第一个。

          【讨论】:

            【解决方案6】:

            尝试在操作方法中使用与路由表 url 参数中相同的参数名称。

            global.asx

            routes.MapRoute(
            
                                    "Default", // Route name
                                    "{controller}/{action}/{id}", // URL with parameters
                                    new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            
                            );
            

            我的控制器

            public ActionResult ErrorPage(int id)
            
                {
                    return View();
                }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2011-06-07
              • 2012-09-12
              • 2023-03-14
              • 1970-01-01
              • 1970-01-01
              • 2019-07-18
              相关资源
              最近更新 更多