why does razor make form with empty action?

why does razor make form with empty action?

I create a form with razor to have specific controller and action with asp-controller and asp-action but after razor render the page, I get form with empty action. I do not know why

<form asp-controller="PanelController" asp-action="AddCatAndSubAndBrand" method="post">

but result in html is

<form method="post" action="">

I put @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers in _viewImports

 

回答

You need to drop the word Controller, if your controller class name is PanelController you just use "Panel". Its a convention. Since it doesn't find the controller based on conventions it doesn't render the action.

<form asp-controller="Panel" asp-action="AddCatAndSubAndBrand" method="post">

 

想要正确render form的action attribute,必须确保对应的Controller和Action存在。并且指定asp-controller的时候要省略Controller后缀

 

遇到类似的问题,需要排查cotroller和action是否存在

 

The Route attribute is important for routing in ASP.NET Core, and its absence could lead to unexpected behavior, including empty URLs in your Razor views.

In ASP.NET Core, the Route attribute is used to specify the route template for an action method. Without it, the framework may not be able to resolve the correct URL for the action, especially if there are multiple actions with similar names or the default route template does not match your intended URL structure.

To fix the issue, you should add the Route attribute to your controller or action method, specifying the route template that corresponds to the desired URL path.

 

作者:Chuck Lu    GitHub    
posted @   ChuckLu  阅读(12)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2020-07-26 What's the replacement for fuslogvw in .net core 2?
2020-07-26 What exactly happens when I set LoadUserProfile of IIS pool?
2020-07-26 DebugView v4.90
2020-07-26 How to track down log4net problems
2020-07-26 IIS: Idle Timeout vs Recycle
2020-07-26 Window.location
2020-07-26 Web technology for developersSee Web APIsStorage
点击右上角即可分享
微信分享提示