Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何设置路由以便索引显示?_C#_.net_Asp.net Mvc 4_Routing - Fatal编程技术网

C# 如何设置路由以便索引显示?

C# 如何设置路由以便索引显示?,c#,.net,asp.net-mvc-4,routing,C#,.net,Asp.net Mvc 4,Routing,所以我知道如果你在多个URL上有相同的内容,谷歌会惩罚一个网站。。。不幸的是,在MVC中,这太常见了,我可以使用example.com/、example.com/Home/和example.com/Home/Index,所有三个URL都会将我带到同一页面。。。因此,我如何确保无论何时索引在url中,它都会重定向到相同的位置,而不使用索引,当然,对于主页,我处理这一问题的方法是,对于像索引这样的默认页面,只需为其中一个页面创建显式路由。例如,“example.com/People”将是People

所以我知道如果你在多个URL上有相同的内容,谷歌会惩罚一个网站。。。不幸的是,在MVC中,这太常见了,我可以使用
example.com/
example.com/Home/
example.com/Home/Index
,所有三个URL都会将我带到同一页面。。。因此,我如何确保无论何时
索引
在url中,它都会重定向到相同的位置,而不使用
索引
,当然,对于
主页
,我处理这一问题的方法是,对于像索引这样的默认页面,只需为其中一个页面创建显式路由。例如,“example.com/People”将是People/Index的路由,并且在url“/example.com/People/Index”处没有有效页面

Home示例的独特之处在于它可能有三个不同的URL。同样,在本例中,我只需为该索引操作创建一个“example.com”路由,而不支持其他两个URL。换句话说,您永远不会链接到URL的其他形式,因此它们的缺失不会导致问题

我们使用名为AttributeRouting的Nuget包来支持这一点。当您为页面指定GET路由时,它将覆盖MVC的默认值

使用AttributeRouting通常您会将索引映射到
[GET(“”)]
,但对于Home的特殊情况,您还希望支持省略控制器名称的根URL,我认为您还应该使用IsaSoluteUrl添加一个附加属性:

public class HomeController : BaseController
{
     [GET("")]
     [GET("", IsAbsoluteUrl = true)]
     public ActionResult Index()
     {...
也许对你有用。 这个库对你来说不是很方便,但应该可以用

var route = routes.MapRoute(name: "Default",
                            url: "{controller}/{action}/{id}",
                            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional });

routes.Redirect(r => r.MapRoute("home_index", "/home/index")).To(route);
routes.Redirect(r => r.MapRoute("home", "/home")).To(route);

所以我找到了一种不用任何外部库的方法

在我的
RouteConfig
中,我必须将这两条路线添加到顶部,
IgnoreRoute

        routes.MapRoute(
            "Root", 
            "Home/",
            new { controller = "Redirect", action = "Home" }
        );

        routes.MapRoute(
            "Index",
            "{action}/Index",
            new { controller = "Redirect", action = "Home" }
        );
然后我必须创建一个新的
控制器
,名为
重定向
,我为其他每个
控制器
创建了一个方法,如下所示:

public class RedirectController : Controller
{
    public ActionResult Home()
    {
        return RedirectPermanent("~/");
    }

    public ActionResult News()
    {
        return RedirectPermanent("~/News/");
    }

    public ActionResult ContactUs()
    {
        return RedirectPermanent("~/ContactUs/");
    }

    // A method for each of my Controllers
}
 routes.MapRoute(
            name: "ContactUs",
            url: "ContactUs/{id}/{action}",
            defaults: new { controller = "ContactUs", action = "Index", id = UrlParameter.Optional }
        );
就是这样,现在我的网站看起来合法了。在我的URL中没有更多的主页,没有更多的索引,这当然有一个限制,即不能接受
控制器的
Index
方法的任何参数,尽管如果确实需要,您应该能够调整它以实现您想要的

仅供参考,如果要将参数传递给索引操作,则可以添加第三条路径,如下所示:

public class RedirectController : Controller
{
    public ActionResult Home()
    {
        return RedirectPermanent("~/");
    }

    public ActionResult News()
    {
        return RedirectPermanent("~/News/");
    }

    public ActionResult ContactUs()
    {
        return RedirectPermanent("~/ContactUs/");
    }

    // A method for each of my Controllers
}
 routes.MapRoute(
            name: "ContactUs",
            url: "ContactUs/{id}/{action}",
            defaults: new { controller = "ContactUs", action = "Index", id = UrlParameter.Optional }
        );

这将创建一个如下URL:
/ContactUs/14

查看规范URL:一个关于如何设置家庭、家庭/索引路由的示例,我们可以一起使用您的示例,这将非常有用…@SerjSagan添加了一个示例也许您误解了我的问题。。。此方法1需要一个外部库,2不起作用。。。它仍然允许我同时进入
主页/
主页/索引/
您几乎得到了公认的答案,但我找到了一种不依赖外部库的方法。。。