asp.net核心剃须刀页面支持删除和放置请求 [英] asp.net core razor pages support for delete and put requests

查看:36
本文介绍了asp.net核心剃须刀页面支持删除和放置请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我在asp.net核心剃须刀页面中了解到有关请求处理的信息,并说它使用约定支持 head 请求:

Recently, I read about request handling in asp.net core razor pages and it says that it supports head requests using the convention:

public void OnHead()
{
}

效果很好.然后我也尝试使用相同的约定删除.

It worked perfectly. And then I also tried delete using the same convention.

public void OnDelete()
{
}

但是,当我使用邮递员发送删除请求时,它返回一个错误的请求(500).我不确定是否需要提供其他配置才能使用删除请求.任何人都可以帮助我.

But when I send a delete request using postman, it returns a bad request (500). I'm not sure if i need to provide additional configurations to use delete requests. Any one could help me pls.

推荐答案

没有 OnDelete / OnPut .这是因为Razor页面直接面向Web视图,即在浏览器标签/窗口中显示的页面.浏览器没有本机发送DELETE/PUT请求的方法,因此没有理由支持它们.而是通过HTML表单元素处理此类任务,该表单元素将通过POST发送.因此,您可以使用 OnPost()进行处理.

There is no OnDelete/OnPut. This is because Razor Pages are directly geared towards web views, i.e. pages displaying in a browser tab/window. There is no native way in a browser to send DELETE/PUT requests, so there's no reason to support them. Instead, such tasks are handled via an HTML form element, which would send via POST. As such, you would use OnPost() to handle it.

文档建议使用其自己的 OnGet OnPost 方法创建一个专门用于删除的Razor页面以进行删除.或者,您可以简单地将其他处理程序以 OnPost [Something] 的形式添加到现有的Razor Page中.对于删除,可能是 OnPostDelete ,而对于更新,您可能具有 OnPostUpdate .名称无所谓,只是您需要将其作为 handler 传递,例如:

The docs recommend creating a new Razor Page for delete, with its own OnGet and OnPost methods, specifically geared to handling deletes. Alternatively, you may simply add an additional handler to an existing Razor Page in the form of OnPost[Something]. For a delete, that would likely be OnPostDelete, while for an update, you'd likely have OnPostUpdate. The name doesn't matter, except that you will need to pass it as a handler such as:

<form asp-page="Foo" asp-handler="Delete">

如果您需要通过瘦客户端( HttpClient ,AJAX,Postman等)进行交互,则应完全避免使用Razor Pages,而应坚持使用完全支持所有HTTP动词的传统控制器.

If you need to interact via a thin client (HttpClient, AJAX, Postman, etc.), then you should avoid Razor Pages altogether and stick with traditional controllers, which fully supports all HTTP verbs.

这篇关于asp.net核心剃须刀页面支持删除和放置请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆