DSF*_*DSF 8 c# asp.net-mvc-routing asp.net-core
由于某些原因,当我在HttpSysWindows服务器上(通过服务结构)运行ASP.NET Core API时,路由无法正常工作,而本地一切正常。问题是中间件工作正常,所以我知道请求正在处理中,但是它永远无法命中任何控制器,它默认为我的app.run("Some 404 response")404中间件。我的一些代码:
启动文件
public void ConfigureServices(IServiceCollection services)
{
    #region IOC
    //ommitted
    #endregion 
    services.AddAutoMapper(typeof(SomeModel));
    services.AddCors(c => 
    {
        c.AddPolicy("AllowOrigin", options => options.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod());
    });
    services.AddDbContext<SomeContext>(options => options.UseSqlServer(_configuration.GetConnectionString("Dev")));
    services.AddMvc().AddFluentValidation();
}        
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
        IdentityModelEventSource.ShowPII = true;
    }
    //Eliminating that auth is the problem
    //app.UseAuthentication(); 
    if (env.IsProduction())
    {
        app.UseHsts();
        app.UseHttpsRedirection();
    }
    app.UseCors("AllowOrigin");
    app.UseMvcWithDefaultRoute(); //tried this instead of below. No luck
    //app.UseMvc();
    app.Use((context, next) =>
    {
        if (context.Request.Path.Value == "" || context.Request.Path.Value == "/")
        {
            context.Response.ContentType = "text/plain";
            return context.Response.WriteAsync("We're running!");
        }
        return next.Invoke();
    });
    app.Run(context =>
    {
        context.Response.StatusCode = 404;
        context.Response.ContentType = "application/json";
        return context.Response.WriteAsync("{ \"message\": \"Not found\" }");
        });
    }
}
Run Code Online (Sandbox Code Playgroud)
Program.cs:
public static void Main(string[] args)
{
    using (var scope = host.Services.CreateScope())
    {
        var services = scope.ServiceProvider;
        try
        {
            var context = services.GetRequiredService<SomeContext>();
            DbInitializer.Initialize(context);
        }
        catch (Exception ex)
        {
            logger.Error(ex, "An error occured while seeding the database");
        }
    }
    host.Run();
}
Run Code Online (Sandbox Code Playgroud)
public static IWebHost CreateWebHostBuilder(string[] args)
{
    IHostingEnvironment env = null;
    var builder = 
        WebHost.CreateDefaultBuilder(args)
        .UseStartup<Startup>()
        .ConfigureAppConfiguration((hostingContext, config) =>
        {
           env = hostingContext.HostingEnvironment;
        })
        .UseHttpSys(options =>
        {
            if (!env.IsDevelopment())
            {
                options.UrlPrefixes.Add("https://*:30010/BasePathOfAPI/");
            }
            else
            {
                options.UrlPrefixes.Add("http://localhost:5000/BasePathOfAPI/");
            }
        })
        .ConfigureLogging(b =>
        {
            b.AddApplicationInsights("id here");
        })
        .UseNLog()
        .Build();
    return builder;
}
Run Code Online (Sandbox Code Playgroud)
因此,除了UrlPrefixes之外,设置几乎类似。我既可以通过网关又可以在Windows Server上调用https://somehost/BasePathOfAPI/并We're running!在浏览器中显示消息,这一事实告诉我API已启动并正在运行,但是如果尝试的话,它根本无法命中任何控制器。控制器的一个示例:
[Route("api/{someNumber:int}/Home")]
[ApiController]
public class HomeController: ControllerBase
{
    //ctor and props ommitted
    [HttpGet("GetSomeData")
    [ProducesResponseType(StatusCodes.200OK)]
    public async Task<IActionResult> GetSomeData()
    {
        //implemenetation
    }
}
Run Code Online (Sandbox Code Playgroud)
现在,我用来尝试访问上述控制器的网址是:
https://somehost/BasePathOfAPI/api/1234/Home/GetSomeData
Run Code Online (Sandbox Code Playgroud)
它返回404消息:找不到,但是如果我在本地运行:
http://localhost:5000/BasePathOfAPI/api/1234/Home/GetSomeData
Run Code Online (Sandbox Code Playgroud)
工作正常。
不知道我要去哪里错了,也许有点问题,UrlPrefixes但是如果那不正确,那么我应该能够使用中间件吗?也许与路由有关,但是为什么它可以在本地工作?
已解决-必须将完整路径库添加到UrlPrefix和urlacl注册中,以便
netsh http add urlacl url=https://*:30010/BasePathOfAPI/ user="NT AUTHORITY\NETWORK SERVICE" 
Run Code Online (Sandbox Code Playgroud)
除此之外,因为控制器位于另一个dll中,所以必须在ConfigureServices方法中引用该程序集:
services.AddMvc().AddApplicationPart(typeof(SystemController).Assembly)
Run Code Online (Sandbox Code Playgroud)
这两个修复程序使其正常工作
|   归档时间:  |  
           
  |  
        
|   查看次数:  |  
           301 次  |  
        
|   最近记录:  |