简体   繁体   English

我的帖子表单没有达到我的 asp.net mvc 核心 web 应用程序中的操作方法

[英]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-controllerasp-actionasp-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.

相关问题 在我的 asp.net 核心 MVC web 应用程序中,我如何使验证码成为必填字段 - Inside my asp.net core MVC web application how i can make the recaptcha a required field ASP.NET Core 2.1 Razor 表单,发布未到达控制器 - ASP.NET Core 2.1 Razor Form, Post not reaching Controller 为什么在ASP.NET MVC 4 Web应用程序中未调用我的操作? - Why is my action not getting called in ASP.NET MVC 4 web application? 我应该如何保护我的 Web 应用程序(ASP.Net Core 3.1 MVC)? - How should i secure my Web Application(ASP.Net Core 3.1 MVC)? 我的 ASP.NET Core MVC 应用程序停止保存更改 - My ASP.NET Core MVC application stopped saving changes 如何为我的ASP.NET Core Web应用程序创建WebJob - How to create a WebJob for my ASP.NET Core Web Application 在我的asp.net mvc Web应用程序中使用带有WebClient()的Parallel.Foreach有任何缺点或风险吗? - are there any drawbacks or risks of using Parallel.Foreach with WebClient() inside my asp.net mvc web application 我的asp.net mvc Web应用程序中的OutputCache设置。 多种语法防止缓存 - OutputCache setting inside my asp.net mvc web application. Multiple syntax to prevent caching 在我的asp.net MVC Web应用程序中获取通用存储库+子存储库+ UnitOfWork - Getting Generic Repository + Child repository + UnitOfWork inside my asp.net mvc web application 从我的asp.net mvc Web应用程序调用asp.net控制台应用程序 - Calling an asp.net console application from my asp.net mvc web application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM