Try our conversational search powered by Generative AI!

POST from CMS Admin MVC page

Vote:
 

Hi Fellows,

I'm implementing a CMS admin page using the ASP.NET MVC 5 for the UI (with EPiServer CMS 11.19). I'm able to include the related CSS and JS in order to use the CMS admin look & feel. My problem is that it seems I'm unable to issue a POST request: on the view, there is a form with an input filed of type "file" and a submit button (input of type "submit"). Clicking the submit button does GET-request to the controller and not the expected POST-request. Thus I'm unable to post the form with the file-field. Regardles of the input control, that should issue the POST-request (anchor; normal button with JS for the onclick event, where the form' submit gets called;), the controller receives a GET-request insted of the POST-one.

How can I issue POST-request with the form?

Many thanks for your attention.

#229958
Oct 27, 2020 18:22
Vote:
 

Have you stated correct method on the form?

<form method="post">
#229987
Oct 28, 2020 8:24
Vote:
 

Hi @Johan Peterson,

my form looks like that bellow:

@using (Html.BeginForm("MyPostAction", "MyController", FormMethod.Post, new {id= "myForm",enctype = "multipart/form-data", @class = "epi-FormArea"}))
{
@Html.AntiForgeryToken()
...
<div class="epi-buttonContainer">
<span class="epi-cmsButton">
<input class="epi-cmsButton-text epi-cmsButton-tools epi-cmsButton-Import" id="submitButton" type="submit" value="Submit">
</span>
<span class="epi-cmsButton">
@* <input class="" id="submitButton2" type="button" value="Import #2" onclick="document.getElementById('myForm').submit()"> *@
<a href="javascript:document.getElementById('myForm').submit()">Submit!</a>
</span>
</div>
}

None of the submit trials above hit the POST-method MyPostAction. That action does not have a HttpGet counterpart, so the error is 404. If I use the Index Action instead (which has HttpGet and HttpPost variants), then the click on the submit-button (link) hits the GET-version of the action.

Many thanks and kind regards.

#229990
Edited, Oct 28, 2020 9:29
Vote:
 

The form seems correct. And are you sure that this is a GET, is that what the browser console (F12 tools) says as well?

#229991
Oct 28, 2020 9:34
Vote:
 

Hi @Johan Peterson,

I can see three entries in the Network tab:

  • first is a POST request with status code 301 (moved permanently), the initiator is "Other"
  • second is a GET request with the same status - 301, the initiator is MyController
  • third is a GET request with status 404 (not found), the initiator is the Route / MyController

This leads me to the conclusion, the route seems to be not correct... I've configured it this way:

RouteTable.Routes.MapRoute(
"MyPlugin",
"MyController/{action}",
new { controller = "MyController", action = "Index" } );

Do you recognize here something wrong?

Many thanks and kind regards.

#229993
Oct 28, 2020 10:20
Vote:
 

Depends on where you have put your controller (in which namespace). Please see https://blog.novacare.no/creating-episerver-admin-plugins-using-mvc/

#229996
Oct 28, 2020 10:53
Vote:
 

Here's the controller's declaration:

namespace MyApplication.Controllers
{
using System.Web.Mvc;
using EPiServer.PlugIn;
using EPiServer.ServiceLocation;
using Models.ViewModels;

[GuiPlugIn(Area = PlugInArea.AdminMenu, DisplayName = "Custom Tool Plugin", Url = "/MyController")]
[Authorize(Roles = "CmsAdmins, WebAdmins")]
public class MyController : Controller
{
...
}

}

#229997
Oct 28, 2020 11:38
Vote:
 

Your controller must have an action called MyPostAction that accepts a posts as well.

#230001
Oct 28, 2020 13:04
Vote:
 

Yes, the controller has that action, also it has the GET+POST Index actions.

#230003
Oct 28, 2020 13:28
Vote:
 

Is the form posted to the correct URL? Either inspect the html and check the action attribute on the form or network tab in the F12 tools.

#230004
Oct 28, 2020 13:32
Vote:
 

I think so - the form gets posted to a URL like this http://localhost:12345/MyController/MyPostAction (or in case of using the Index action http://localhost:12345/MyController). But the question here would be why the POST comes from "Other" and gets responded with 301 from the server... To my understanding, this would really mean the controller has not been configured correctly... But how to configure it correctly?! And finally then why the GET request goes straight to the right controller action? Like on the same controller the GET is correct, the POST is wrong... 

#230006
Edited, Oct 28, 2020 13:43
Vote:
 

Do you have any rewrite rules that are doing redirects? A 404 handler? Other routes that triggers before this one? Try putting this route first in the route table.

#230007
Oct 28, 2020 13:49
Vote:
 

For handling 404-errors, I'm using the BVN 404 Handler:

Web.config:
<bvn404Handler handlerMode="On">
</bvn404Handler>

And I also have a redirect.config with redirect rules (good catch from your side!), whose last action on the configured rule (one single rule has been configured) is:

<action type="Redirect" url="{ToLower:{URL}}" redirectType="Permanent" />

And this seems the reason why the redirection gets status 301 (permanent) and it seems the controller MyController has not been listed in the conditions block of the rule... I'll look at this and will change the rule's conditions to see if when I add the controller to the conditions, it will start to behave correctly... Will report here as soon as I've results...

Many thanks and kind regards.

#230009
Oct 28, 2020 14:09
Vote:
 

Thanks @Johan Peterson, for your help on solving my problem.

I can confirm the problem has been solved after the redirect rule has been extended with a condition for the MyController route.

Kind regards.

#230010
Oct 28, 2020 15:09
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.