我无法安装 system.net.http
I can't install system.net.http
我在 .net 4.6.1
中有一个 web api 2
项目,我试图在其中添加自定义 ValidateModelAttribute
但我收到了 system.net.http 4.0
程序集的警告。
这是程序集属性
我曾多次尝试删除并重新安装该软件包,但我一直收到相同的警告。当我尝试使用 CustomModelAttribute
时,来自 Postman 的 api 调用会抛出以下错误
{
"Message": "An error has occurred.",
"ExceptionMessage": "Method not found: 'System.Net.Http.HttpRequestMessage
System.Web.Http.Controllers.HttpActionContext.get_Request()'.",
"ExceptionType": "System.MissingMethodException",
"StackTrace": " at myapi.Api.Mvc.Filters.ValidateModelAttribute.OnActionExecuting(HttpActionContext
actionContext)\r\n at
System.Web.Http.Filters.ActionFilterAttribute.OnActionExecutingAsync(HttpActionContext
actionContext, CancellationToken cancellationToken)\r\n--- End of
stack trace from previous location where exception was thrown ---\r\n
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task
task)\r\n at
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task
task)\r\n at
System.Web.Http.Filters.ActionFilterAttribute.d__0.MoveNext()\r\n---
End of stack trace from previous location where exception was thrown
---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task
task)\r\n at
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task
task)\r\n at
System.Web.Http.Controllers.ActionFilterResult.d__2.MoveNext()\r\n---
End of stack trace from previous location where exception was thrown
---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task
task)\r\n at
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task
task)\r\n at
System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()"
}
这是自定义属性
using System.Net;
using System.Web.Http.Controllers;
using System.Net.Http;
using System.Web.Http.Filters;
namespace myapi.Api.Mvc.Filters
{
public class ValidateModelAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext actionContext)
{
if (actionContext.ModelState.IsValid == false)
{
actionContext.Response = actionContext.Request.CreateErrorResponse(
HttpStatusCode.BadRequest, actionContext.ModelState);
}
}
}
}
我终于让它工作了。我所要做的就是右键单击 system.net.http
程序集并单击属性。然后我将 copy local
设置为 true
我在 .net 4.6.1
中有一个 web api 2
项目,我试图在其中添加自定义 ValidateModelAttribute
但我收到了 system.net.http 4.0
程序集的警告。
这是程序集属性
我曾多次尝试删除并重新安装该软件包,但我一直收到相同的警告。当我尝试使用 CustomModelAttribute
时,来自 Postman 的 api 调用会抛出以下错误
{
"Message": "An error has occurred.",
"ExceptionMessage": "Method not found: 'System.Net.Http.HttpRequestMessage System.Web.Http.Controllers.HttpActionContext.get_Request()'.",
"ExceptionType": "System.MissingMethodException",
"StackTrace": " at myapi.Api.Mvc.Filters.ValidateModelAttribute.OnActionExecuting(HttpActionContext actionContext)\r\n at System.Web.Http.Filters.ActionFilterAttribute.OnActionExecutingAsync(HttpActionContext actionContext, CancellationToken cancellationToken)\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Filters.ActionFilterAttribute.d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Controllers.ActionFilterResult.d__2.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()" }
这是自定义属性
using System.Net;
using System.Web.Http.Controllers;
using System.Net.Http;
using System.Web.Http.Filters;
namespace myapi.Api.Mvc.Filters
{
public class ValidateModelAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext actionContext)
{
if (actionContext.ModelState.IsValid == false)
{
actionContext.Response = actionContext.Request.CreateErrorResponse(
HttpStatusCode.BadRequest, actionContext.ModelState);
}
}
}
}
我终于让它工作了。我所要做的就是右键单击 system.net.http
程序集并单击属性。然后我将 copy local
设置为 true