2

after migrating my app from .NET Framework 4.8 to .NET6, Html.BeginForm has started to change the slash into "%2F" 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%2FReport/DownloadReport" enctype="multipart/form-data" method="post">

Example of code where it happens:

<div class="form-container">
                @using (Html.BeginForm("DownloadReport", "Admin/Report", FormMethod.Post, new { enctype = "multipart/form-data" }))
                {
                    @Html.Hidden("requestReportName", "PageReport");
                    <span class="epi-cmsButton">
                        <input class="epi-cmsButton-text epi-cmsButton-tools epi-cmsButton-Export" 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}");
cowefe
  • 213
  • 1
  • 12
  • Check what the second parameter of BeginForm expects. It looks like it should be ControllerName. So you have a controller named "Admin/Report"? – thewallrus Aug 16 '22 at 12:41
  • In the folder Controllers I have another folder called "Admin", in which there is a controller called "AdminController" and that's how it has worked before the migration. – cowefe Aug 16 '22 at 12:53
  • @cowefe: The second parameter in the `Html.BeginForm()` is defined as `"Admin/Report"`. You told that `Admin` is name of the folder under `Controllers`. And what is the `Report` means? – Jackdaw Aug 16 '22 at 13:03
  • Sorry, I wrote it wrong, there is no AdminController, but there is ReportController. – cowefe Aug 16 '22 at 13:06
  • @cowefe: Do you have your own custom `IControllerFactory` interface implementation? – Jackdaw Aug 16 '22 at 13:18
  • Also, posting your Route mapping would be helpful. – thewallrus Aug 16 '22 at 13:21
  • @Victor No, I don't have any implementation of that interface. – cowefe Aug 16 '22 at 13:24
  • 1
    @thewallrus I have updated my question with route mapping for controllers in admin folder. – cowefe Aug 16 '22 at 13:26

1 Answers1

2

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

Jackdaw
  • 7,626
  • 5
  • 15
  • 33