Web Api???????д??
???????????? ???????[ 2014/4/29 15:12:52 ] ??????????????? Web Api
??????????????Web Api??????и?Controller
public class SomeController : ApiController
{
public HttpResponseMessage Get()
{
// ?Щ????
return Request.CreateResponse(HttpStatusCode.OK?? someModel);
}
}
????????????????????????? SomeController ??Get()???????????????????Exception???Request?Null??
???????????????????????й??????Request???????????
????1????????
[TestMethod]
public void UnitTestMethod()
{
// ???????????
YourNameSpace.Controllers.SomeController controller = new SomeController();
// ?????????????????????????????
controller.Request = new HttpRequestMessage();
controller.Request.SetConfiguration(new HttpConfiguration());
var result = controller.Get();
// ????
}
????2??????????????
[TestMethod]
public void UnitTestMethod()
{
// ???????????
YourNameSpace.Controllers.SomeController controller = new SomeController();
var config = new HttpConfiguration();
var request = new HttpRequestMessage(HttpMethod.Post?? "YourUrl");
var route = config.Routes.MapHttpRoute("DefaultApi"?? "api/{controller}/{id}");
var routeData = new HttpRouteData(route?? new HttpRouteValueDictionary { { "controller"?? "products" } });
controller.ControllerContext = new HttpControllerContext(config?? routeData?? request);
controller.Request = request;
controller.Request.Properties[HttpPropertyKeys.HttpConfigurationKey] = config;
var result = controller.Get();
// ????
}
?????????????е?????????????
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11