[英]MVC5 Route Doesn't Work
I recently upgraded from MVC 5 to MVC 3. I never registred a route, however, this URL worked: 我最近从MVC 5升级到了MVC3。我从未注册过路由,但是,此URL有效:
http://www.testsite.com/shipworks/index/myemail@yahoo.com?website=testsite.com http://www.testsite.com/shipworks/index/myemail@yahoo.com?website=testsite.com
Here is my code: 这是我的代码:
[HttpPost]
public ActionResult Index(string id, string website)
{
string data = string.Empty;
}
I now get a 404 with this code. 我现在得到一个带有此代码的404。 I tried this route, however, it also fails:
我尝试了此路线,但是,它也失败了:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"ShipWorks", // Route name
"{controller}/{action}/{id}/{website}", // URL with parameters
new { controller = "ShipWorks", action = "Index", email = UrlParameter.Optional, website =
UrlParameter.Optional }, new[] { "CloudCartConnector.Web.Controllers" }// Parameter defaults
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
What am I doing wrong? 我究竟做错了什么? I put the shipping route above the default one.
我将运输路线设置为默认路线之上。 Note that my id is a string.
请注意,我的ID是一个字符串。 The URL works fine with ?id=myemail@myemail.com.
该URL可与?id=myemail@myemail.com配合使用。
First approach. 第一种方法。
According to your path: 根据您的路径:
"{controller}/{action}/{id}/{website}"
you don't need to specify explicitly "website" property name. 您无需明确指定“网站”属性名称。 Then, you marked in your route that
{id}
and {website}
are separated by slash /
, so correct use of your route mask should be http://www.testsite.com/shipworks/index/myemail@yahoo.com/testsite.com
. 然后,您在路由中标记了
{id}
和{website}
用斜杠/
分隔,因此正确使用路由掩码应该是http://www.testsite.com/shipworks/index/myemail@yahoo.com/testsite.com
。
However, here is one problem - dot symbol .
但是,这是一个问题-点符号
.
will not be recognized correctly (in the way you want). 将无法正确识别(以您想要的方式)。 So if you remove the dot, the path
http://www.testsite.com/shipworks/index/myemail@yahoo.com/testsitecom
will work. 因此,如果删除该点,则
http://www.testsite.com/shipworks/index/myemail@yahoo.com/testsitecom
路径将起作用。
Second approach. 第二种方法。
In order to reach the results you want, you'd better pass e-mail and website as the query parameter, not as the part of the path. 为了获得所需的结果,最好将电子邮件和网站作为查询参数而不是路径的一部分。
You can use the following route config: 您可以使用以下路由配置:
routes.MapRoute(
"ShipWorks", // Route name
"{controller}/{action}/{id}", // URL with parameters
new
{
controller = "ShipWorks",
action = "Index",
id = UrlParameter.Optional
}, new[] { "CloudCartConnector.Web.Controllers" }// Parameter defaults
);
Controller's action: 管制员的行动:
//[HttpPost]
public ActionResult Index(string email, string website)
{
(...)
return View();
}
And the query string: http://www.testsite.com/shipworks/index?email=myemail@yahoo.com&website=testsite.com
. 以及查询字符串:
http://www.testsite.com/shipworks/index?email=myemail@yahoo.com&website=testsite.com
: http://www.testsite.com/shipworks/index?email=myemail@yahoo.com&website=testsite.com
。
Please note also, that since you marked your Index
method with [HttpPost]
attribute, you will get 404
using GET
method (like typing in browser) even if you have correct URL. 另请注意,由于您用
[HttpPost]
属性标记了Index
方法,因此即使您具有正确的URL,也将使用GET
方法(例如在浏览器中键入)来获取404
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.