我可以在 asp net razor 项目类型中使用 MVC 控制器吗?
我刚刚开始使用 Razor 页面而不是 MVC 方法。
在我的剃刀页面中,我有一个组合框,用户可以在其中切换语言
<form asp-action="ChangeLanguage" asp-controller="Home" method="post">
<input type="hidden" id="returnUrl" name="returnUrl" value="@Model.returnUrl" />
<select id="culture"
name="culture"
class="form-control"
onchange="this.form.submit();"
asp-items="@Model.cultureItems"
asp-for="@Model.requestCulture.RequestCulture.UICulture.Name">
</select>
</form>
该组合框填充了可用语言,没有问题。
当我选择下拉列表时,会执行一个帖子,但我的控制器中的方法永远不会收到请求
public class HomeController : Controller
{
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult ChangeLanguage(string culture, string returnUrl)
{
Response.Cookies.Append(
CookieRequestCultureProvider.DefaultCookieName,
CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(culture)),
new CookieOptions
{
Expires = DateTimeOffset.UtcNow.AddDays(7)
}
);
return LocalRedirect(returnUrl);
}
}
检查请求后,帖子网址似乎无效
Request URL: https://localhost:44350/
Request Method: POST
Status Code: 200
Remote Address: [::1]:44350
Referrer Policy: strict-origin-when-cross-origin
我在这里遗漏了什么吗? 一般来说,我可以在 Razor 页面项目类型中使用 MVC 控制器吗?
渲染的 html 是
<form method="post" action="">
<input type="hidden" id="returnUrl" name="returnUrl" value="/">
<select id="culture" name="culture" class="" onchange="this.form.submit();" data-val="true" data-val-required="The Name field is required.">
<option value="el-GR">Ελληνικά (Ελλάδα)</option>
<option selected="selected" value="en-US">Αγγλικά (Ηνωμένες Πολιτείες)</option>
</select>
<input name="__RequestVerificationToken" type="hidden" value="removed_for_simplicity">
</form>
另一个更新正在更改表单 as
<form method="post" action="/Home/ChangeLanguage">
和 using
[HttpPost]
[ValidateAntiForgeryToken]
[Route("Home/ChangeLanguage")]
public IActionResult ChangeLanguage(string culture, string returnUrl)
但现在我有一个 404 错误
I just getting started using Razor pages instead of the MVC approach.
Within my razor page i have a combobox where the user can switch language
<form asp-action="ChangeLanguage" asp-controller="Home" method="post">
<input type="hidden" id="returnUrl" name="returnUrl" value="@Model.returnUrl" />
<select id="culture"
name="culture"
class="form-control"
onchange="this.form.submit();"
asp-items="@Model.cultureItems"
asp-for="@Model.requestCulture.RequestCulture.UICulture.Name">
</select>
</form>
The combobox is populated with available languages without issues.
When i select the dropdown a post is performed but the method within my controller, never gets the request
public class HomeController : Controller
{
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult ChangeLanguage(string culture, string returnUrl)
{
Response.Cookies.Append(
CookieRequestCultureProvider.DefaultCookieName,
CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(culture)),
new CookieOptions
{
Expires = DateTimeOffset.UtcNow.AddDays(7)
}
);
return LocalRedirect(returnUrl);
}
}
After inspecting the request, the post url seems invalid
Request URL: https://localhost:44350/
Request Method: POST
Status Code: 200
Remote Address: [::1]:44350
Referrer Policy: strict-origin-when-cross-origin
Am i missing something here?
Generally speaking can i use MVC controllers in Razor pages project types?
The rendered html is
<form method="post" action="">
<input type="hidden" id="returnUrl" name="returnUrl" value="/">
<select id="culture" name="culture" class="" onchange="this.form.submit();" data-val="true" data-val-required="The Name field is required.">
<option value="el-GR">Ελληνικά (Ελλάδα)</option>
<option selected="selected" value="en-US">Αγγλικά (Ηνωμένες Πολιτείες)</option>
</select>
<input name="__RequestVerificationToken" type="hidden" value="removed_for_simplicity">
</form>
Another update is changing the form as
<form method="post" action="/Home/ChangeLanguage">
and using
[HttpPost]
[ValidateAntiForgeryToken]
[Route("Home/ChangeLanguage")]
public IActionResult ChangeLanguage(string culture, string returnUrl)
but now i have an 404 error
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我终于可以在 razor 页面中使用 MVC 控制器了。
Startup
文件中的MapControllers()
扩展方法发挥了神奇作用。I can finally use MVC controllers in razor pages.
The
MapControllers()
extension method in theStartup
file does the magic.