ASP.NET 核心路线变更

ASP.NET Core Route Change

我需要更新一些静态网页,我想花时间在 [=50= 中使用 ASP.NET 核心(ASP.NET 5 和 MVC 6)重新创建它们] 2015.我想用微软的最新技术重建它,让以后的改变更容易。

当我在本地主机上启动项目时,默认网站加载正常,但任何 linked 页面都会中断,因为它们默认路由到 /Home 控制器。另外,项目的jquerycssimages[=]的none 42=]是MVC嵌套这些页面时发现的

文件Startup.cs中有如下方法:

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    loggerFactory.AddConsole(Configuration.GetSection("Logging"));
    loggerFactory.AddDebug();

    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
        app.UseDatabaseErrorPage();
        app.UseBrowserLink();
    }
    else
    {
        app.UseExceptionHandler("/Home/Error");
    }

    app.UseStaticFiles();

    app.UseIdentity();

    // Add external authentication middleware below. To configure them please see http://go.microsoft.com/fwlink/?LinkID=532715

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

这是开箱即用的配置方式。

我们公司不希望 /Home(或其他任何内容)停留在其所有网页的 URL 上。

我为不同的页面创建了各种 IActionResult 方法,但它们都做同样的事情:

public IActionResult Index()
{
    return View();
}

我们也有公司 link 到我们的网站。更改我们页面的结构将导致其他公司也停止并进行更改。

我如何获取一个字符串,例如 {controller}/{action}/{id?},并使 return 成为典型的 link,例如 action.aspx?id=x

或者,有没有办法告诉 MVC 不要对某些页面使用模板?

如果这是愚蠢的基础,我深表歉意。我通常处理 Windows 表格。

有两种解决方案:

路线模板:

app.UseMvc(routes =>
{
    routes.MapRoute(
        "HomeRoute", 
        "{action}/{id}", 
        new 
        { 
            controller = "Home", 
            action = "Index", 
            id = UrlParameter.Optional
        });
});

属性路由

[HttpGet("")]
public IActionResult Index()
{
    return View();
}

您可以使用 ASP.NET MVC Boilerplate 创建一个新项目以获得使用此方法的完整工作示例。