MVC 主页路由默认始终为 /shopping



我试图在主页上默认我的路线并使其显示在Google上。

我的网站www.timefor.com,但我总是希望它显示www.timefor.com/shopping

默认控制器正常为主页/索引。 但是当我运行它时,该网站显示为"www.timefor.com">

如何让购物被默认。

我尝试更改默认路线图,但没有运气。

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
    namespaces: new[] { "MyNamespace.Controllers" }
);

首页控制器

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View("Index");
    }
}

当我开始项目时,我希望URL为:

www.timefor.com/shopping

这只能使用 http 重定向来实现。

1. 让我们从/购物开始工作:

将当前HomeController重命名为 ShoppingController

运行项目并将/shopping 附加到 url - 它应该显示您的首页。

2. 执行重定向

创建新HomeController 。然后更改它,使其看起来像这样:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return RedirectPermanent("/shopping");
    }
}

现在,每当用户访问您的网站时,他们都会被重定向到/shopping。

通过使用永久重定向,像谷歌这样的搜索引擎应该使用/shopping URL 索引您的页面。

最新更新