[英]c# hosted rest service return 404
我已經在 c# 中實現了我的控制器
[ServiceContract(Name = "UserController")]
public interface ApiController
{
Task<HttpResponseMessage> SignUp(LoginModel model);
Task<HttpResponseMessage> Login(LoginModel model);
}
namespace Nerder_Backend.Controllers
{
[RoutePrefix("api/user")]
public class UserController : ApiController
{
private readonly IBucket _bucket = ClusterHelper.GetBucket(ConfigurationManager.AppSettings.Get("CouchbaseUserBucket"));
private readonly string _secretKey = ConfigurationManager.AppSettings["JWTTokenSecret"];
[Route("signup")]
[HttpPost]
public async Task<HttpResponseMessage> SignUp(LoginModel model)
{
if (model == null || !model.IsValid())
{
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.BadRequest)
{
Content = new StringContent("Invalid email and/or password")
};
return response;
}
然后我嘗試托管它以便可以在 jquery 上通過 AJAX 調用它,因為我的應用程序將是移動跨平台的,我必須將客戶端與服務器端分開,無論如何,這是托管的主要方法:
static void Main(string[] args)
{
var baseAddress = "http://localhost:8080";
var config = new HttpSelfHostConfiguration(baseAddress);
CouchbaseConfig.Register();
GlobalConfiguration.Configure(WebApiConfig.Register);
config.MapHttpAttributeRoutes();//map attribute routes
// Create the server
using (var server = new HttpSelfHostServer(config))
{
server.OpenAsync().Wait();
Console.WriteLine("The service is ready at {0}", baseAddress);
Console.WriteLine("Press <Enter> to stop the service.");
Console.ReadLine();
}
}
主機啟動並正在偵聽,但是當我通過 javascript 調用 URL 時,我收到 404 錯誤消息。
HTTP 狀態碼 404。
這是javascript代碼:
$.ajax({
type: 'post',
url: 'http://localhost:8080/api/user/signup',
我還使用了 chrome 的 CORS 啟用程序,如何檢查我的路徑是否有效? 我應該改變托管服務的方式嗎?
這是我的 app.config 如果它可以幫助:
<system.webServer>
<!-- ENABLE REST SERVICES -->
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Content-Type" />
<add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
</customHeaders>
</httpProtocol>
<!--END -->
</system.webServer>
<system.web>
<compilation debug="true" targetFramework="4.5.2" />
<httpRuntime targetFramework="4.5.2" />
</system.web>
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" />
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+" />
</compilers>
</system.codedom>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-9.0.0.0" newVersion="9.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Couchbase.NetClient" publicKeyToken="05e9c6b5a9ec94c2" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.4.5.0" newVersion="2.4.5.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Castle.Core" publicKeyToken="407dd0808d44fbdc" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<system.webServer>
<handlers>
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>
</configuration>
您如何托管您的服務?
當服務運行時,您如何確保端口實際上是 8080? - 它可能會給出 404 錯誤,因為它是錯誤的端口
例如,如果您的服務僅存在於 Visual Studio 中,並且您按 F5 開始調試,它會自動在您的瀏覽器中為您打開一個新窗口,並且您會在右下角任務欄區域看到“IIS Express”圖標:端口可能會有所不同。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.