C# Request.CreateErrorResponse 在哪里?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12555795/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 23:40:27  来源:igfitidea点击:

Where is Request.CreateErrorResponse?

c#.netasp.net-web-api

提问by Gary McGill

I saw it in this blog post, but that doesn't actually say how to "enable" it. And it seems that by default it isn't enabled.

我在这篇博文中看到了它,但这实际上并没有说明如何“启用”它。似乎默认情况下它没有启用。

I know it's an extension method, as defined here: http://msdn.microsoft.com/en-us/library/hh835786(v=vs.108).aspxbut how do I get access to it? If I type Request.CreateErrorResponsethen the compiler doesn't recognize it.

我知道这是一种扩展方法,定义如下:http: //msdn.microsoft.com/en-us/library/hh835786(v=vs.108).aspx但我如何访问它?如果我输入,Request.CreateErrorResponse则编译器无法识别它。

I'm already using System.Net.Http.

我已经using System.Net.Http

采纳答案by AlexGad

Are you still using pre-release or release version? There were a number of extensions that did not appear until just before the release and did not exist in earlier versions of the webapi release. I am unsure if this was one of them, but it may be what is causing your problem.

你还在使用预发行版还是发行版?有许多扩展直到发布之前才出现,并且在 webapi 发布的早期版本中也不存在。我不确定这是否是其中之一,但这可能是导致您出现问题的原因。

回答by Christofer Eliasson

My best guess is that it is the third assembly, that you haven't referenced, that is the problem.

我最好的猜测是它是第三个程序集,你没有引用,这就是问题所在。

Unfortunately I don't have access to my computer, browsing on my phone, so I can't track down the file for you. According to the docs it should be in System.Web.Http.SelfHost.dll, so I guess you could just search for that file to locate it.

不幸的是,我无法访问我的计算机,无法在手机上浏览,因此我无法为您找到该文件。根据文档,它应该在 System.Web.Http.SelfHost.dll 中,所以我想您可以搜索该文件来定位它。

回答by DevYev

Request is the public property of the ApiController class and should be available to you in any API actions in that controller.

Request 是 ApiController 类的公共属性,您应该可以在该控制器中的任何 API 操作中使用它。

public abstract class ApiController
{
   ...
   public HttpRequestMessage Request { get; set; }
}

Here is a small code sample that works for me:

这是一个对我有用的小代码示例:

using System.Net;
using System.Net.Http;
using System.Web.Http;


public class MyFirstApiController : ApiController
{
    // GET 
    public HttpResponseMessage Get(int id)
    {
        return Request.CreateErrorResponse(HttpStatusCode.ExpectationFailed, "Some   message here");
    }
}

Interestingly enough, if I take away using System.Net.Http; statement, Request no longer has a CreateErrorResponse() method.

有趣的是,如果我使用 System.Net.Http; 语句,Request 不再有 CreateErrorResponse() 方法。

回答by KeithR

I've noticed this in release version as well. And if you don't have the right using statement, it'll error. You need :

我在发布版本中也注意到了这一点。如果你没有正确的 using 语句,它就会出错。你需要 :

using System.Net.Http;

even if you already have this:

即使你已经有了这个:

using System.Web.Http.Controllers;

回答by Sandro Stadler

Use System.Web.Httpassembly. source: CreateErrorResponse Method

使用System.Web.Http程序集。来源:CreateErrorResponse 方法

回答by kaarthick raman

Although important points have been answered.

虽然已经回答了重要的问题。

One more dumbest possibility is that you might have chosen wrong type of controller. Make sure you have created an Web API controller, incase of MVC controller you cannot have

一种更愚蠢的可能性是您可能选择了错误类型的控制器。确保你已经创建了一个Web API 控制器,以防万一你不能拥有 MVC 控制器

Request.CreateErrorResponse()

Just another possibility where you may find this issue.

这是您可能会发现此问题的另一种可能性。

回答by AminGolmahalle

you can use from that In block Try-Catch.

 [HttpGet]
    public HttpResponseMessage DownloadVideo(Guid id, string title)
    {
        try
        {
          //Your Code at the end return result
        }
        catch (Exception ex)
        {
            return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex.Message);
        }
    }