简体   繁体   English

Request.CreateErrorResponse 在哪里?

[英]Where is Request.CreateErrorResponse?

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).aspx but how do I get access to it?我知道这是一种扩展方法,如此处定义:http://msdn.microsoft.com/en-us/library/hh835786(v=vs.108).aspx但我如何才能访问它? If I type Request.CreateErrorResponse then the compiler doesn't recognize it.如果我输入Request.CreateErrorResponse那么编译器将无法识别它。

I'm already using System.Net.Http .我已经在using System.Net.Http

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 System.Net.Http;

even if you already have this: 即使你已经拥有这个:

using System.Web.Http.Controllers;

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; 有趣的是,如果我带走使用System.Net.Http; statement, Request no longer has a CreateErrorResponse() method. 声明,Request不再具有CreateErrorResponse()方法。

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. 有许多扩展直到发布之前才出现,并且在早期版本的webapi发行版中不存在。 I am unsure if this was one of them, but it may be what is causing your problem. 我不确定这是否是其中之一,但它可能是导致你的问题的原因。

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. 您可能会发现此问题的另一种可能性。

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);
        }
    }

Use System.Web.Http assembly. 使用System.Web.Http程序集。 source: CreateErrorResponse Method source: CreateErrorResponse方法

Inject and instance of the HttpRequestMessage in your controller constructor or method:在 controller 构造函数或方法中注入和实例化 HttpRequestMessage:

Task<ActionResult> GetOne(HttpRequestMessage request,int shopId){ Task<ActionResult> GetOne(HttpRequestMessage request,int shopId){

return request.CreateErrorResponse(.....) }返回 request.CreateErrorResponse(.....) }

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中,所以我想你可以只搜索该文件来找到它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM