繁体   English   中英

c#托管休息服务返回404

[英]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=\&quot;Web\&quot; /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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM