ASP.NET核心路由更改 [英] ASP.NET Core Route Change

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

问题描述

我需要更新一些静态网页,我想借此时间在Visual Studio 2015中使用ASP.NET Core(带有MVC 6的ASP.NET 5)重新创建它们.我想使用Microsoft的最新技术使将来的更改变得更加容易.

I need to update a handful of static web pages, and I would like to take this time to recreate them using ASP.NET Core (ASP.NET 5 with MVC 6) in Visual Studio 2015. I want to rebuild it using Microsoft's latest technology to make changes easier in the future.

当我在localhost上启动项目时,默认网站可以正常加载,但是任何链接的页面均会中断,因为它们默认情况下会路由到/Home控制器.此外,当MVC嵌套这些页面时,找不到项目的 jquery css 图像.

When I start the project on localhost, the default website loads up fine but any of the linked pages break because they route to the /Home controller by default. Also, none of the project's jquery, css or images are found when MVC nests these pages.

在文件 Startup.cs 中,有以下方法:

In the file Startup.cs, there is the following method:

// 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?}");
    });
}

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

This is how it comes configured out of the box.

我们公司不希望将/Home(或其他任何内容)粘贴在其所有网页的URL上.

Our company does not want /Home (or anything else) stuck on the URL of all of their web pages.

我为不同的页面创建了各种 IActionResult 方法,但是它们都具有相同的作用:

I have created various IActionResult methods for the different pages, but all of them do the same thing:

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

我们还有链接到我们网站的公司.更改页面结构将导致其他公司也停下来进行更改.

We also have companies that link to our website. Changing the structure of our pages is going to cause other companies to stop and make changes, too.

我该如何获取一个字符串,例如{controller}/{action}/{id?},并使其返回一个典型的链接,例如action.aspx?id=x?

How would I take a string, such as {controller}/{action}/{id?}, and have that return a typical link, such as action.aspx?id=x?

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

Alternately, is there a way to tell MVC not to use a template for certain pages?

如果这是愚蠢的基本做法,我深表歉意.我通常使用Windows窗体.

I apologize if this is stupid basic. I generally work on Windows Forms.

推荐答案

有两种解决方案:

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 创建一个新项目,使用此方法的完整示例.

You can create a new project using ASP.NET MVC Boilerplate for a full working example using this method.

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

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