2013-11-26 14 views
5

tôi đọc http://www.asp.net/web-api/overview/security/working-with-ssl-in-web-api và cố gắng sử dụng mã từ trang đó:HttpResponseMessage không làm việc trong Api Web (.NET 4.5)

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Net; 
using System.Net.Http; 
using System.Web; 
using System.Web.Http; 
using System.Web.Http.Filters; 
using System.Web.Http.Controllers; 

public class RequireHttpsAttribute : AuthorizationFilterAttribute 
{ 
    public override void OnAuthorization(HttpActionContext actionContext) 
    { 
     if (actionContext.Request.RequestUri.Scheme != Uri.UriSchemeHttps) 
     { 
      actionContext.Response = new HttpResponseMessage(System.Net.HttpStatusCode.Forbidden) 
      { 
       ReasonPhrase = "HTTPS Required" 
      }; 
     } 
     else 
     { 
      base.OnAuthorization(actionContext); 
     } 
    } 
} 

Khi tôi xây dựng tôi không nhận được một lỗi, nhưng thời gian chạy cho lỗi này :

CS0012: The type 'System.Net.Http.HttpRequestMessage' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.

Tôi đã tham chiếu đến System.Net.Http trong thư mục tham chiếu của dự án của tôi. Nếu tôi nhìn vào các thuộc tính của nó, nó nói Version 4.0.0.0 và Runtime Version 4.0.30319. Các thuộc tính dự án của tôi nói khung đích là .NET 4.5.

IntelliSense của tôi trong VS2013 Express cũng không muốn nhận bất kỳ điều gì liên quan đến HttpResponseMessage hoặc HttpRequestMessage.

Tôi đã thử xóa tham chiếu và thêm lại tham chiếu nhưng không có kết quả.

Mọi trợ giúp sẽ được đánh giá cao.

Trả lời

8

Trong web.config bạn hãy thử thêm: lắp ráp thêm

<configuration> 
    <system.web> 
     <compilation> 
      <assemblies> 
       <add assembly="System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/> 
      </assemblies> 
      </compilation> 
    </system.web> 
</configuration> 
+1

Đừng quên phần tử 'biên soạn' ở giữa' system.web' và 'assembly'. –

0

Cố gắng ràng buộc trong tập tin cấu hình

<runtime> 
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> 
    <dependentAssembly> 
     <assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" /> 
     <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" /> 
    </dependentAssembly> 
    </assemblyBinding> 
</runtime 
4

Vâng, tôi có thể bị trễ, nhưng chỉ trong trường hợp người khác phải đối mặt với vấn đề này.

Trước hết, bạn cần phải tìm một chuỗi:

<compilation debug="true" targetFramework="4.5"/> 

Và làm cho nó không thẻ tự đóng như thế:

<compilation debug="true" targetFramework="4.5"> 
</compilation> 

Tiếp theo, thêm cụm từ khóa bên trong với thông tin lắp ráp bạn trước đây, nên nó trông giống như sau:

<compilation debug="true" targetFramework="4.5"> 
     <assemblies> 
     <add assembly="System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/> 
     </assemblies> 
</compilation> 

Và xây dựng lại giải pháp của bạn. Hãy xem this link.

+0

Điều này đã làm cho các trick cho tôi. Nó biên dịch không có vấn đề, nhưng intelliSense cho thấy nó như là một lỗi trong công cụ xem dao cạo cho đến khi tôi khởi động lại Visual Studio. –

Các vấn đề liên quan