Asp.net核心默认路由 [英] Asp.net core default route

查看:109
本文介绍了Asp.net核心默认路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简化的启动代码:

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()的结果.只是空白页.我想念什么?

After running application in Visual Studio 2015 I see in browser "localhost:xxx", but I don't see result of MainController.Index(). Just blank page. What did I miss?

更新:

Web.config:

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:

问题来自于向控制器注入依赖项的异常,因为我忘记了使用开发人员异常页面站点,只是向我返回了空白页面.因此,对于错误的问题,我们深表歉意,但是对于我来说,路由是可以的.

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.

这篇关于Asp.net核心默认路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆