Home > Net >  BeginForm messes up controller path value in .NET 6
BeginForm messes up controller path value in .NET 6

Time:08-17

after migrating my app from .NET Framework 4.8 to .NET6, Html.BeginForm has started to change the slash into "/" in the controller path, which causes problems because then they become unreachable.

Basically, this:

<form action="/Admin/Report/DownloadReport" enctype="multipart/form-data" method="post">

Becomes this:

<form action="/Admin/Report/DownloadReport" enctype="multipart/form-data" method="post">

Example of code where it happens:

<div >
                @using (Html.BeginForm("DownloadReport", "Admin/Report", FormMethod.Post, new { enctype = "multipart/form-data" }))
                {
                    @Html.Hidden("requestReportName", "PageReport");
                    <span >
                        <input  type="submit" name="btnSubmitDownloadPageReport" id="btnSubmitDownloadPageReport" value="Download Report" />
                    </span>}
</div>

What can be the cause of that strange behavior? I have not found any information that Html.Beginform has became obsolete in .NET6.

Edit: My route mapping:

endpoints.MapControllerRoute(
                    name: "Admin",
                    pattern: "Admin/{controller}/{action=Index}");

CodePudding user response:

ASP.NET MVC

If you are working with the Built-In Controller Factory using controller name in format Admin/Report actually does not correct. When the built-in controller factory looking for a controller it uses controller name and looking for Your_Controller_NameController class in YourApp.Controllers.* namespaces.

It is possible to map the specific URL route by defining different namespaces using the namespaces parameter of the MapRoute() method in the ASP.NET MVC.

Try to fix the controller name in the @using (Html.BeginForm("DownloadReport", "Admin/Report", FormMethod.Post, new { enctype = "multipart/form-data" })) to Report. I suppose your application will find the DownloadReport action method without a problem.

See RouteCollectionExtensions.MapRoute Method

ASP.NET Core

I suppose after fixed the controller name in the Html.BeginForm("DownloadReport", "Admin/Report"...) to "Report" your application migration will work correct.

If you will have routing problems provide more information or see the following post: Restrict route to controller namespace in ASP.NET Core

  • Related