[英]My Post form is not reaching the action method inside my asp.net mvc core web application
I am working on an asp.net MVC core web application, and i have the following routing rules:-我正在研究 asp.net MVC 核心 web 应用程序,并且我有以下路由规则:-
app.UseEndpoints(endpoints => {
endpoints.MapControllerRoute(
name: "IP",
pattern: "IP/{controller=Home}/{action=Index}/{id?}");
});
app.UseEndpoints(endpoints => {
endpoints.MapControllerRoute(
name: "Info",
pattern: "Info/{controller=Home}/{action=Index}/{id?}");
});
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapRazorPages();
});
then inside my create form i define the following to use different url when posting back the form depending on the current url:-然后在我的创建表单中,我定义以下内容以在根据当前 url 发回表单时使用不同的 url:-
<form method="post" action="@(!Context.Request.Path.Value.ToString().ToLower().Contains("/info") ? "/IP/submissions/create/": "/info/submissions/create/")" >
and here is my post action method:-这是我的后期操作方法:-
[AllowAnonymous]
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("Submission,SubmissionQuestionSubmission")] SubmissionCreate sc )
{
but currently when i post back the form the action method will not be called... any idea?但是目前,当我回发表单时,不会调用操作方法……有什么想法吗? and the browser console will raise this error:-
并且浏览器控制台将引发此错误:-
HTTP400: BAD REQUEST - The request could not be processed by the server due to invalid syntax.
POST - https://localhost:44363/info/Submissions/Create/
It's a "Bad Request" because you aren't using the form tag helper .这是一个“错误请求”,因为您没有使用表单标签助手。 Which should build the action url for you and add an anti forgery token.
哪个应该为您构建操作 url 并添加防伪令牌。
However, your routes and controller actions should have a 1-1 mapping.但是,您的路线和 controller 操作应该具有 1-1 映射。 Right now you have 3 different routes to the same actions.
现在,您有 3 条不同的路线来执行相同的操作。 How are those actions supposed to know which route was used and therefore what action to take?
这些动作应该如何知道使用了哪条路由,因此应该采取什么动作? Are you planning on checking the raw url in each action?
您是否计划在每个操作中检查原始 url?
Are you attempting to split your controllers into areas ?您是否试图将控制器分成多个区域? In which case adding the
asp-controller
, asp-action
, asp-route-...
attributes will cause the form tag helper to build the right url for you.在这种情况下,添加
asp-controller
、 asp-action
、 asp-route-...
属性将导致表单标签助手为您构建正确的 url。
Or would you prefer to add an extra argument to each action, indicating which url was used.或者您更愿意为每个操作添加一个额外的参数,指示使用了哪个 url。 Which you could then add to your model so your view can provide it as a route parameter.
然后您可以将其添加到 model 中,以便您的视图可以将其作为路由参数提供。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.