Problem with ASP.Net MVC routing using Html.BeginForm

Using MVC, I have a html form helper in my view:

using (Html.BeginForm("ActionOne", "ControllerOne")) ...

Using the default route, the output for the action attribute is as expected:

<form action="/ControllerOne/ActionOne" ...

But registering a new route with an apparent lack of matches affects the result.

Routing Code:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.Add("testRoute", new Route("MyUrl", new MvcRouteHandler()));

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

Output:

<form action="/MyUrl?action=ActionOne&amp;controller=ControllerOne"

Is it design or am I doing something fundamental?

Hurrah!

+3
source share
3 answers

I experienced this exact problem. I'm not sure why System.Web.Mvc.HtmlHelper just uses the first route without ignoring it in the routed link generator, etc., but I found a workaround for the "BeginForm" problem.

Global.asax.cs, :

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

Html.BeginFormRoute MVC " ", , URL:

using (Html.BeginRouteForm("Default", new { controller="YourController", action = "YourFormAction" })) { }

+9

public static void RegisterRoutes(RouteCollection routes)
{
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.Add("testRoute", new Route("MyUrl/***{action}/{controller}***", new MvcRouteHandler()));

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

, .

+1

Add this before the default route

      routes.MapRoute("", "ControllerOne/ActionOne", new { controller = "ControllerOne", action = "ActionOneOne"});
-1
source

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


All Articles