【问题标题】:Asp.net core default routeAsp.net核心默认路由
【发布时间】:2017-03-02 11:57:16
【问题描述】:

简体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() 的结果。只是空白页。我错过了什么?

更新:

Web.config:

<?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:

问题来自于对控制器的依赖注入服务中的异常,并且因为我忘记使用开发人员异常页面站点刚刚返回空白页面给我。所以对于不正确的问题我很抱歉,但在我的情况下路由很好。

【问题讨论】:

  • 检查我的答案,如果对你有帮助,请告诉我!

标签: c# .net routing asp.net-core


【解决方案1】:
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 部分停止工作,我只能获取 css 文件和图像等静态资源。
  • @KovpaevAlexey docs.asp.net/en/latest/fundamentals/routing.html 检查此网址。这几乎是 .net core 中路由的基础知识。
  • 问题来自对控制器的依赖注入服务中的异常,并且因为我忘记使用开发人员异常页面站点刚刚返回空白页面给我。所以对于不正确的问题我很抱歉,但在我的情况下路由很好。
  • @KovpaevAlexey 很好,一切正常,如果需要,您可以将问题标记为正确。
【解决方案2】:

对我来说最简单的方法(并且不使用 MVC)是使用空的 [Route("")] custum 属性将控制器设置为默认路由,如下所示:

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

与 Startup.Configure

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

【讨论】:

    【解决方案3】:

    对于所有获得空白页的人,请将 PreserveCompilationContext 设置为 true:

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

    在 csproj 中 vs 2017 或

    "buildOptions": {   "preserveCompilationContext": true }
    

    在 project.json 中

    【讨论】:

      【解决方案4】:

      在 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 documentation

      【讨论】:

        【解决方案5】:

        另一种解决方案是将“/”重定向到另一个网址

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-11-01
          • 1970-01-01
          • 2021-09-07
          • 1970-01-01
          相关资源
          最近更新 更多