Why is ASP.NET-MVC Routing UrlParameter.Optional ignored when using this regular expression?

This is a stripped-down example of the problem that I encountered today with ASP.NET MVC URL routing.

Quite simply, I need a route to call, regardless of whether a parameter was provided at the end.

This route works fine, matching both / apple / and / apple / test /

routes.MapRoute (
    "Working Route",
    "apple / {parameter}",
    new { 
        controller = "Apple", 
        action = "Action", 
        parameter = UrlParameter.Optional
    },
    new {parameter = @ "([a-z0-9 \ .-] +)"}
);

However, this second route will only match / banana / test / and the like. When I try to run / banana / , the router just goes right above it and returns a 404 catch-all error.

routes.MapRoute (
    Non-Working Route,
    "banana / {parameter}",
    new { 
        controller = "Banana", 
        action = "Action", 
        parameter = UrlParameter.Optional
    },
    new {parameter = @ "([a-z0-9] +)"}
);

The only difference is checking the Regex parameter, but since this is a pretty simple Regex match, they should work fine for a url like / banana / , but the second route just doesn't work to recognize it.

, Regex №2, №1, '.' '-', , -, .

EDIT:

, . .

public class AppleController : Controller
{
    public ActionResult Action(string parameter)
    {
        if (parameter == null)
        {
            parameter = "No parameter specified.";
        }
        ViewData["parameter"] = parameter;
        return View();
    }
}

public class BananaController : Controller
{
    public ActionResult Action(string parameter)
    {
        if (parameter == null)
        {
            parameter = "No parameter specified.";
        }
        ViewData["parameter"] = parameter;
        return View();
    }
}

, , /apple/ "No parameter defined.", /banana/ 404.

</" > ..

= URLParameter.Optional : № 1 , №2 .

= "" : №1, №2 , URL.

= "" : - .NET.

.

+3
2

, , , , . (foo)?.

</" > ASP.NET MVC 2 .NET 4. ASP.NET MVC 3, , . , , ASP.NET MVC 2, ASP.NET MVC 3, , v2, ​​ v3.

v2 web.config v3:

<runtime>
   <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
         <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
         <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
   </assemblyBinding>
</runtime>
+4

2nd EDIT

~// ~ /

routes.MapRoute(
    "Working Route",
    "Fruit/{fruit}",
    new
    {
        controller = "Fruit",
        action = "Index",
        fruit = ""
    },
    new { fruit = @"([a-z0-9\.-]+)" }
);

public ActionResult Index(string fruit)
{
    ViewData["fruit"] = !String.IsNullOrEmpty(fruit) ? fruit : "None specified.";
    return View();
}

~/banana/ ~/banana/yellow

routes.MapRoute(
    "Non-Working Route",
    "Banana/{color}",
    new
    {
        controller = "Banana",
        action = "Index",
        color = ""
    },
    new { color = @"([a-z0-9]+)" }
);

public ActionResult Index(string color)
    {
        ViewData["Color"] = color;
        return View();
    }

1st EDIT

, : string parameter = ""


. , ( 404, "-" , ). ?

( /) :

routes.MapRoute(
    "Working Route",
    "Fruit/{fruit}",
    new
    {
        controller = "Fruit",
        action = "Index",
        fruit = UrlParameter.Optional
    },
    new { fruit = @"([a-z0-9]+)" }
);

Index() :

public ActionResult Index(string fruit = "")
{
    ViewData["fruit"] = fruit;
    return View();
}
0

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


All Articles