Kov*_*xey 16 .net c# routing asp.net-core
简化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" });
});
}
Run Code Online (Sandbox Code Playgroud)
在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>
Run Code Online (Sandbox Code Playgroud)
更新2:
问题来自依赖注入服务到控制器的异常,因为我忘记使用开发人员异常页面站点只是向我返回空白页面.所以我很抱歉不正确的问题,但在我的情况下,路由很好.
myb*_*ame 12
routes.MapRoute(
name: "default",
template: "{controller}/{action}/{id?}",
defaults: new { controller = "Main", action = "Index" });
routes.MapRoute(
name: "default",
template: "{controller=Main}/{action=Index}/{id?}");
Run Code Online (Sandbox Code Playgroud)
这是定义默认路由的两种方法.你正在混合它们.您需要始终定义模板.在第二种方式中,您可以直接在模板中编写默认值.
对我来说最简单的方法(不使用 MVC)是使用空的 [Route("")] custum 属性将控制器设置为默认路由,如下所示:
[ApiController]
[Route("")]
[Route("[controller]")]
public class MainController : ControllerBase
{ ... }
Run Code Online (Sandbox Code Playgroud)
使用启动.配置
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
24423 次 |
| 最近记录: |