ASP.NET MVC 4 some routes do not work

Route:

routes.MapRoute( "Customer_widget", "customer/widget/{action}/{id}", new { controller = "Customer_Widget", id = UrlParameter.Optional }); 

test URL1: (works) customer/widget/contact_list/1-1004-SC-0-0-0-0-0-0-Supplier-Supplier----0-0-0-0-Year-Calendar-0-Home-0

test URL2: (does not work)

 customer/widget/contact_list/1-1004-SC-0-0-0-0-0-0-Supplier-Supplier--6%2f1%2f2013-7%2f6%2f2013--0-0-0-0-Year-Calendar-0-Home-0 (does not work) 

I have the same two URLs above. The first URL goes to the right place. But the second URL just lost its way ... I do not know what causes this ... I assume that the daytime part, 6% 2f1% 2f2013-7% 2f6% 2f2013, causes some problems, but I'm not sure if this such.

CustomerController

  public ActionResult Index(string id = null) { string temp = "~/customer/widget/contact_list/" + this.objURL.ToString(); return Redirect("~/customer/widget/contact_list/" + this.objURL.ToString()); } 

Customer_WidgetController

  public ActionResult Contact_list(string id = null) { return PartialView("_contact_list",Customer_Widget.Contact_list.Load(id, ref errors)); } 

CustomerController flow → (along the map route) Customer_WidgetController

+4
source share
1 answer

This is all due to the encoded character "% 2f" corresponding to "/". Because of this, your url

 customer/widget/contact_list/1-1004-SC-0-0-0-0-0-0-Supplier-Supplier--6%2f1%2f2013-7%2f6%2f2013--0-0-0-0-Year-Calendar-0-Home-0 

It is divided into 8 segments:

  • customer
  • widget
  • contact_list
  • 1-1004-SC-0-0-0-0-0-0-0-supplier-supplier - 6
  • 1
  • 2013-7
  • 6
  • 2013--0-0-0-0-Year-calendar-0-Home-0

but on your route you expect 4.

To determine the number of variable segments, you can use an asterisk (*) as follows:

 routes.MapRoute( "Customer_widget", "customer/widget/{action}/{*id}", new { controller = "Customer_Widget", id = UrlParameter.Optional }); 

The routing system checks the routes sequentially. Therefore, you need to be careful with this and define the route as low as possible, because it can catch a request that you do not want to catch using this route. For example, if the following route is defined on your system after the route above, it will never be snatched:

 routes.MapRoute( "Customer_widget", "customer/widget/{action}/{lang}/{*id}", new { controller = "Customer_Widget", lang = "en", id = UrlParameter.Optional } new { lang = "en|es|ru"}); 
0
source

Source: https://habr.com/ru/post/1490099/


All Articles