如何强制asp.net MVC 2重定向到默认的控制器/操作

时间:2022-05-27 17:07:25

In a brand new ASP.NET MVC2 project, I want the user to be redirected to

在全新的ASP中。NET MVC2项目,我希望用户重定向到。

http://<mysite>/home/index 

rather than

而不是

http://<mysite>/

We do this with our other sites for tracking purposes, to avoid the scenario where hits to the same default page show up as

我们使用其他站点来进行跟踪,以避免出现相同默认页面的情况。

http://<mysite>/
http://<mysite>/default.aspx

How do I accomplish this so that http://<mysite>/ automatically redirects to whatever default controller/action I have set up in my routing? Please note that I am aware the two are functionally equivalent, as the default controller action will be executed either way. I'm just interested in forcing consistent URLs in the browser.

如何做到这一点,使http:// /自动重定向到我在路由中设置的默认控制器/操作?请注意,我知道这两者在功能上是等价的,因为默认控制器操作将以任何一种方式执行。我只是想在浏览器中强制使用一致的url。

1 个解决方案

#1


2  

Something like this should do it, no?

像这样的东西应该可以,不是吗?

public class HomeController : Controller
{
    public ActionResult Index()
    {
        string incomingUrl = HttpContext.Request.Url.LocalPath;

        if (incomingUrl == "/")
        {
            return Redirect("/home/index");
        }
        else
        {
            return View();
        }
    }
}

#1


2  

Something like this should do it, no?

像这样的东西应该可以,不是吗?

public class HomeController : Controller
{
    public ActionResult Index()
    {
        string incomingUrl = HttpContext.Request.Url.LocalPath;

        if (incomingUrl == "/")
        {
            return Redirect("/home/index");
        }
        else
        {
            return View();
        }
    }
}