I am working on a website in asp.net mvc. I have a route
我正在asp.net mvc的一个网站上工作。我有路
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
// Parameter defaults
);
which is the default route. And I have a method
这是默认路由。我有一个方法
public ActionResult ErrorPage(int errorno)
{
return View();
}
Now if I want to run this code with http://something/mycontroller/Errorpage/1 it doesn't work. But if I change the parameter name to id from errorno
it works.
现在,如果我想用http:// something / mycontroller / Errorpage / 1运行此代码,它就不起作用了。但是,如果我将参数名称从errorno更改为id,则可以正常工作。
Is it compulsory to have same parameter name for this method? Or do I need to create separate routes for such situations?
是否必须为此方法使用相同的参数名称?或者我是否需要为这种情况创建单独的路线?
6 个解决方案
#1
15
So, you have a parameter named errorno
, and you want it to have a value from parameter id
. This is obviously the binding problem.
因此,您有一个名为errorno的参数,并且您希望它具有参数id的值。这显然是绑定问题。
How to solve it:
如何解决:
-
create a class for model binder:
为模型绑定器创建一个类:
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; } }
-
create a custom attribute for custom model binding:
为自定义模型绑定创建自定义属性:
[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); } }
-
apply the new attribute to your action parameters as needed:
根据需要将新属性应用于您的操作参数:
public ActionResult ErrorPage( [BindParameter("id")] int errorno) { return View(); }
Now your errorno
will have the value, which was passed as id
for your url.
现在,您的errorno将具有值,该值已作为您的网址的ID传递。
Note: you can remove the paramter id
from the example above, if you are sure you need it solved only for id
.
注意:您可以从上面的示例中删除参数ID,如果您确定只需要为id解决它。
Leaving this way will allow you bind other parameters too.
采用这种方式也可以绑定其他参数。
#2
16
Option 1
选项1
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
public ActionResult ErrorPage(int id)
{
return View();
}
Option 2
选项2
routes.MapRoute(
"Default",
"{controller}/{action}/{errorno}",
new { controller = "Home", action = "Index", errorno = UrlParameter.Optional }
);
public ActionResult ErrorPage(int errorno)
{
return View();
}
Option 3
选项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
4
use the bind attribute prefix:
使用绑定属性前缀:
public ActionResult Customer([Bind(Prefix = "id")]string cname) {}
#4
1
@Parminder The default route can handle all action with one parameter "id". And I think not every action need this parameter. So I change my default route
@Parminder默认路由可以使用一个参数“id”处理所有操作。我认为不是每个动作都需要这个参数。所以我改变了我的默认路线
routes.MapRoute(
"Default",
"{controller}/{action}",
new { controller = "Home", action = "Index"}
);
and you can add a new route:
你可以添加一个新的路线:
routes.MapRoute("errorpage", "yourcontroller/errorpage/{errorno}",
new {controller="controllername", action="errorpage"});
this just handle your controll name is "controllername". If you want to handle all controller, you can add this:
这只是处理你的控件名称是“controllername”。如果要处理所有控制器,可以添加:
routes.MapRoute("errorpage", "{controller}/errorpage/{errorno}",
new {controller="controllername", action="errorpage"});
This method will create very much code in global.asax if you need a lot of custom route.
如果您需要大量自定义路由,此方法将在global.asax中创建非常多的代码。
#5
0
You could either rename the parameter in the default root (which probably is not a good idea) or rename it in the action method. Adding another root will not help because it will be the same as the default one and given an url the routing engine cannot distinguish between the two and will always take the first one in the list.
您可以在默认根目录中重命名参数(这可能不是一个好主意),也可以在操作方法中重命名该参数。添加另一个根将无济于事,因为它将与默认根相同,并且给定一个URL,路由引擎无法区分两者并且将始终采用列表中的第一个。
#6
0
try to use the same name of parameter in action method as in in the route table url parameter.
尝试在action table url参数中使用与action方法相同的参数名称。
global.asx
global.asx
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
myController
myController的
public ActionResult ErrorPage(int id)
{
return View();
}
#1
15
So, you have a parameter named errorno
, and you want it to have a value from parameter id
. This is obviously the binding problem.
因此,您有一个名为errorno的参数,并且您希望它具有参数id的值。这显然是绑定问题。
How to solve it:
如何解决:
-
create a class for model binder:
为模型绑定器创建一个类:
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; } }
-
create a custom attribute for custom model binding:
为自定义模型绑定创建自定义属性:
[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); } }
-
apply the new attribute to your action parameters as needed:
根据需要将新属性应用于您的操作参数:
public ActionResult ErrorPage( [BindParameter("id")] int errorno) { return View(); }
Now your errorno
will have the value, which was passed as id
for your url.
现在,您的errorno将具有值,该值已作为您的网址的ID传递。
Note: you can remove the paramter id
from the example above, if you are sure you need it solved only for id
.
注意:您可以从上面的示例中删除参数ID,如果您确定只需要为id解决它。
Leaving this way will allow you bind other parameters too.
采用这种方式也可以绑定其他参数。
#2
16
Option 1
选项1
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
public ActionResult ErrorPage(int id)
{
return View();
}
Option 2
选项2
routes.MapRoute(
"Default",
"{controller}/{action}/{errorno}",
new { controller = "Home", action = "Index", errorno = UrlParameter.Optional }
);
public ActionResult ErrorPage(int errorno)
{
return View();
}
Option 3
选项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
4
use the bind attribute prefix:
使用绑定属性前缀:
public ActionResult Customer([Bind(Prefix = "id")]string cname) {}
#4
1
@Parminder The default route can handle all action with one parameter "id". And I think not every action need this parameter. So I change my default route
@Parminder默认路由可以使用一个参数“id”处理所有操作。我认为不是每个动作都需要这个参数。所以我改变了我的默认路线
routes.MapRoute(
"Default",
"{controller}/{action}",
new { controller = "Home", action = "Index"}
);
and you can add a new route:
你可以添加一个新的路线:
routes.MapRoute("errorpage", "yourcontroller/errorpage/{errorno}",
new {controller="controllername", action="errorpage"});
this just handle your controll name is "controllername". If you want to handle all controller, you can add this:
这只是处理你的控件名称是“controllername”。如果要处理所有控制器,可以添加:
routes.MapRoute("errorpage", "{controller}/errorpage/{errorno}",
new {controller="controllername", action="errorpage"});
This method will create very much code in global.asax if you need a lot of custom route.
如果您需要大量自定义路由,此方法将在global.asax中创建非常多的代码。
#5
0
You could either rename the parameter in the default root (which probably is not a good idea) or rename it in the action method. Adding another root will not help because it will be the same as the default one and given an url the routing engine cannot distinguish between the two and will always take the first one in the list.
您可以在默认根目录中重命名参数(这可能不是一个好主意),也可以在操作方法中重命名该参数。添加另一个根将无济于事,因为它将与默认根相同,并且给定一个URL,路由引擎无法区分两者并且将始终采用列表中的第一个。
#6
0
try to use the same name of parameter in action method as in in the route table url parameter.
尝试在action table url参数中使用与action方法相同的参数名称。
global.asx
global.asx
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
myController
myController的
public ActionResult ErrorPage(int id)
{
return View();
}