如何在ApiController中对HttpResponseMessage.Headers.Location进行填充? [英] How to shim HttpResponseMessage.Headers.Location in an ApiController?
问题描述
我使用的是Microsft假货,它适用于我们需要进行测试的所有内容,但我无法填充(或存根)
I'm using Microsft fakes and it works for everything we are implemting tests for except I can't shim (or stub)
HttpResponseMessage&.Header.Location
HttpResponseMessage .Headers.Location
//下面列出的代码
HttpResponseMessage响应;
HttpResponseMessage response;
response = Request.CreateResponse(HttpStatusCode.Created,uploadResult,new JsonMediaTypeFormatter()); //效果很好
r esponse.Headers.Location = new Uri("xxx"); //始终获得Location = null....
response = Request.CreateResponse(HttpStatusCode.Created, uploadResult, new JsonMediaTypeFormatter()); // Works fine
response.Headers.Location = new Uri("xxx"); // Always get Location = null....
如何在下面的代码中设置标头中的位置,该代码定义了我们的垫片?
//垫片看起来像这样
HttpRequestMessage requestMessage = new HttpRequestMessage(方法,"http://localhost/api/" + apiUri);
HttpRequestMessage requestMessage = new HttpRequestMessage(method, "http://localhost/api/" + apiUri);
MultipartFormDataContent multipartContent = new MultipartFormDataContent(" ---- Boundary ----));
multipartContent.Headers.ContentLocation =新的Uri("http://localhost/api/" + apiUri);
ByteArrayContent content =新的ByteArrayContent(fileContents);
content.Headers.Add("Content-Type","image/jpeg");
multipartContent.Add(content,"content","test file.jpg");
MultipartFormDataContent multipartContent = new MultipartFormDataContent("----Boundary----");
multipartContent.Headers.ContentLocation = new Uri("http://localhost/api/" + apiUri);
ByteArrayContent content = new ByteArrayContent(fileContents);
content.Headers.Add("Content-Type", "image/jpeg");
multipartContent.Add(content, "content", "test file.jpg");
requestMessage.Content = multipartContent;
requestMessage.Content = multipartContent;
IHttpRoute route = configuration.Routes.MapHttpRoute("DefaultApi","api/{controller}/{id}");
HttpRouteData routeData =新的HttpRouteData(路由,新的HttpRouteValueDictionary {{"controller",controllerName},{"action",actionName}}));
IHttpRoute route = configuration.Routes.MapHttpRoute("DefaultApi", "api/{controller}/{id}");
HttpRouteData routeData = new HttpRouteData(route, new HttpRouteValueDictionary { { "controller", controllerName }, { "action", actionName } });
HttpControllerContext context =新的HttpControllerContext(配置,routeData,requestMessage);
ApiController controller.ControllerContext = context;
controller.Request = requestMessage;
controller.Request.Properties [HttpPropertyKeys.HttpConfigurationKey] =配置;
HttpControllerContext context = new HttpControllerContext(configuration, routeData, requestMessage);
ApiController controller.ControllerContext = context;
controller.Request = requestMessage;
controller.Request.Properties[HttpPropertyKeys.HttpConfigurationKey] = configuration;
推荐答案
您好,Agnes,跨度>
此博客: ASP.NET Web Api,在设计真正的Restful服务时伪造了ApiController ,其中描述了伪造ApiController的详细步骤.该博客中有详细的代码段.
This blog: ASP.NET Web Api, Faking ApiController in designing true Restful services described detailed steps to fake ApiController. There are detailed code snippets in that blog.
作者使用以下代码定义了位置:
response.Headers.Location =新的Uri(Url.Link("DefaultApi",新的{id = customer.Id}));
response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = customer.Id }));
希望它可以为您提供帮助.如果没有帮助,您可以随时回来,并向我们提供更多信息.
Hoping it can help you. You can feel free to come back if it is not helpful and provide us more information.
最好的问候
这篇关于如何在ApiController中对HttpResponseMessage.Headers.Location进行填充?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!