Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Asp.net核心默认路由_C#_.net_Routing_Asp.net Core - Fatal编程技术网

C# Asp.net核心默认路由

C# Asp.net核心默认路由,c#,.net,routing,asp.net-core,C#,.net,Routing,Asp.net Core,简化的启动代码: public void ConfigureServices(IServiceCollection services) { services.AddMvc(); } public void Configure(IApplicationBuilder app, IHostingEnvironment env) { app.UseMvc(routes => { routes.MapRoute( name: "defaul

简化的
启动
代码:

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:


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

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

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

以下是定义默认路由的两种方法。你在混合它们您需要始终定义模板。第二种方法是,您可以直接在模板中写入默认值。

对于所有将空白页面集PreserveComilationContext设置为true的用户:

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

在Startup.cs类的project.json中,使用一个方便的方法:使用mvcWithDefaultRoute()

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?}");
   });
}
更多信息请参见

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

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


检查我的答案,告诉我是否帮助了你!这些对我毫无帮助。我仍然在发现这个问题。经过一些更改后,整个mvc部件停止工作,我只能获得css文件和图像等静态资源。@KovpaevAlexey检查此url。这是关于.NETCore路由的基本知识。问题来自于控制器依赖注入服务中的异常,因为我忘记了使用开发者异常页面,所以网站刚刚返回空白页面给我。很抱歉,我的问题不正确,但在我的情况下,路由是可以的。@KovpaevAlexey很好,一切正常,如果你愿意,你可以将问题标记为正确。
[ApiController]
[Route("")]
[Route("[controller]")]
public class MainController : ControllerBase
{ ... }
app.UseRouting();
app.UseEndpoints(endpoints =>
{
    endpoints.MapControllers();
});