Simplified Startup
code:
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(). 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:
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.
答案 0 :(得分:11)
routes.MapRoute(
name: "default",
template: "{controller}/{action}/{id?}",
defaults: new { controller = "Main", action = "Index" });
routes.MapRoute(
name: "default",
template: "{controller=Main}/{action=Index}/{id?}");
This 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.
答案 1 :(得分:1)
对我来说,最简单的方法(不使用MVC)是使用空的[Route(“”)]] custum属性将控制器设置为默认路由,如下所示:
[ApiController]
[Route("")]
[Route("[controller]")]
public class MainController : ControllerBase
{ ... }
使用 Startup.Configure
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
答案 2 :(得分:0)
对于获得空白页面的所有人,将PreserveCompilationContext设置为true:
<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework>
<PreserveCompilationContext>true</PreserveCompilationContext>
</PropertyGroup>
in csproj in vs 2017或
"buildOptions": { "preserveCompilationContext": true }
在project.json中
答案 3 :(得分:0)
在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中的更多信息