简体   繁体   English

Asp.net核心默认路由

[英]Asp.net core default route

Simplified Startup code:简化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" });
    });
}

After running application in Visual Studio 2015 I see in browser "localhost:xxx", but I don't see result of MainController.Index().在 Visual Studio 2015 中运行应用程序后,我在浏览器中看到“localhost:xxx”,但没有看到 MainController.Index() 的结果。 Just blank page.只是空白页。 What did I miss?我错过了什么?

Update:更新:

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>

Update 2:更新 2:

The problem comes from exception in dependency injected service to controller and because I forget to use developer exception page site just returned blank page to me.问题来自对控制器的依赖注入服务中的异常,并且因为我忘记使用开发人员异常页面站点刚刚返回空白页面给我。 So I'm sorry for incorrect question, but routing is fine in my case.所以对于不正确的问题我很抱歉,但在我的情况下路由很好。

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

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

These are the two ways of defining default route.这是定义默认路由的两种方式。 You are mixing them.你正在混合它们。 You need always to define a template.您总是需要定义一个模板。 In the second way you can write the defaults directly in the template.在第二种方式中,您可以直接在模板中编写默认值。

The easiest way for me (and without using MVC) was to set the controller to default route using empty [Route("")] custum attribute like so:对我来说最简单的方法(并且不使用 MVC)是使用空的 [Route("")] custum 属性将控制器设置为默认路由,如下所示:

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

with Startup.Configure使用Startup.Configure

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

Another solution would be to redirect "/" to another url另一种解决方案是将“/”重定向到另一个 url

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

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

For all of you who get blank page set PreserveCompilationContext to true:对于所有获得空白页的人,请将 PreserveCompilationContext 设置为 true:

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

in csproj in vs 2017 or在 vs 2017 中的 csproj 或

"buildOptions": {   "preserveCompilationContext": true }

in project.json在项目.json

In Startup.cs class, use a convenient Method: UseMvcWithDefaultRoute() :在 Startup.cs 类中,使用方便的方法: UseMvcWithDefaultRoute()

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

Can be used to change:可以用来改变:


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

More info in Microsoft documentation Microsoft 文档中的更多信息

For anyone stumbling over this and wanting to serve static default content (single page app hosted in kestrel or whatever) there is also an extension method MapFallbackToFile that can be used like this in Program.cs or Startup.cs对于遇到此问题并希望提供静态默认内容(托管在 kestrel 中的单页应用程序或其他)的任何人,还有一个扩展方法MapFallbackToFile可以在 Program.cs 或 Startup.cs 中像这样使用

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

app is an IApplicationBuilder that you should already have somewhere in your startup code. app是一个IApplicationBuilder ,你应该已经在你的启动代码中的某个地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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