簡體   English   中英

Asp.net核心默認路由

[英]Asp.net core default route

簡化Startup代碼:

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    app.UseMvc(routes =>
    {
        routes.MapRoute(
        name: "default",
        template: "",
        defaults: new { controller = "Main", action = "Index" });
    });
}

在 Visual Studio 2015 中運行應用程序后,我在瀏覽器中看到“localhost:xxx”,但沒有看到 MainController.Index() 的結果。 只是空白頁。 我錯過了什么?

更新:

網絡配置:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <handlers>
      <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified"/>
    </handlers>
    <aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false"/>
  </system.webServer>
</configuration>

更新 2:

問題來自對控制器的依賴注入服務中的異常,並且因為我忘記使用開發人員異常頁面站點剛剛返回空白頁面給我。 所以對於不正確的問題我很抱歉,但在我的情況下路由很好。

routes.MapRoute(
    name: "default",
    template: "{controller}/{action}/{id?}",
    defaults: new { controller = "Main", action = "Index" });

routes.MapRoute(
    name: "default",
    template: "{controller=Main}/{action=Index}/{id?}");

這是定義默認路由的兩種方式。 你正在混合它們。 您總是需要定義一個模板。 在第二種方式中,您可以直接在模板中編寫默認值。

對我來說最簡單的方法(並且不使用 MVC)是使用空的 [Route("")] custum 屬性將控制器設置為默認路由,如下所示:

[ApiController]
[Route("")]
[Route("[controller]")]
public class MainController : ControllerBase
{ ... }

使用Startup.Configure

app.UseRouting();
app.UseEndpoints(endpoints =>
{
    endpoints.MapControllers();
});

另一種解決方案是將“/”重定向到另一個 url

 app.UseEndpoints(endpoints =>
        {
            endpoints.MapDefaultControllerRoute();

            endpoints.MapGet("/", context =>
            {
                return Task.Run(() => context.Response.Redirect("/Account/Login"));
            });
        });

對於所有獲得空白頁的人,請將 PreserveCompilationContext 設置為 true:

  <PropertyGroup>
    <TargetFramework>netcoreapp1.1</TargetFramework>
    <PreserveCompilationContext>true</PreserveCompilationContext>
  </PropertyGroup>

在 vs 2017 中的 csproj 或

"buildOptions": {   "preserveCompilationContext": true }

在項目.json

在 Startup.cs 類中,使用方便的方法: UseMvcWithDefaultRoute()

public void Configure(IApplicationBuilder app, IHostingEnvironment 
{
   app.UseMvcWithDefaultRoute();
}

可以用來改變:


public void Configure(IApplicationBuilder app, IHostingEnvironment 
{
   app.UseMvc(routes =>
   {
      routes.MapRoute("default", "{controller=Home}/{action=Index}/{id?}");
   });
}

Microsoft 文檔中的更多信息

對於遇到此問題並希望提供靜態默認內容(托管在 kestrel 中的單頁應用程序或其他)的任何人,還有一個擴展方法MapFallbackToFile可以在 Program.cs 或 Startup.cs 中像這樣使用

app.UseEndpoints(
  endpoints =>
  {
    endpoints.MapControllers();  // not needed for this sample
    endpoints.MapFallbackToFile("index.html");
  });

app是一個IApplicationBuilder ,你應該已經在你的啟動代碼中的某個地方。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM