C# 路由URL必须以'/';
我已在主控制器中声明索引操作:C# 路由URL必须以'/';,c#,asp.net-mvc,routes,url-routing,asp.net-mvc-routing,C#,Asp.net Mvc,Routes,Url Routing,Asp.net Mvc Routing,我已在主控制器中声明索引操作: [HttpGet] public ActionResult Index(string type) { if (string.IsNullOrEmpty(type)) { return RedirectToAction("Index", new { type = "promotion" }); } return View(); } 它接受: https://localhost:44300/home/index?type=prom
[HttpGet]
public ActionResult Index(string type)
{
if (string.IsNullOrEmpty(type))
{
return RedirectToAction("Index", new { type = "promotion" });
}
return View();
}
它接受:
https://localhost:44300/home/index?type=promotion
及
在我为404页面配置路由之前,一切正常:
routes.MapRoute(
name: "homepage",
url: "home/index",
defaults: new { controller = "Home", action = "Index" }
);
routes.MapRoute(
name: "default",
url: "/",
defaults: new { controller = "Home", action = "Index" }
);
routes.MapRoute(
"404-PageNotFound",
"{*url}",
new { controller = "Error", action = "PageNotFound" }
);
无效语法:
路由URL不能以“/”或“~”字符开头,并且不能
包含一个“?”字符
如果我删除第二个配置
https://localhost:44300/?type=promotion
不会被接受的。->显示404页
我的问题是:有没有一种方法可以配置路由URL以“/”开头(无控制器,无操作)?您的路由配置错误,因为错误表明它不能以
/
开头,对于主页,它不需要。在这种情况下,它应该是一个空字符串
routes.MapRoute(
name: "default",
url: "",
defaults: new { controller = "Home", action = "Index" }
);
然而,当你正在做的时候,想要将多条路线映射到网站主页是有点不寻常的(而且不利于SEO)
重定向到主页也是不常见的,这会在网络上进行额外的往返。通常直接路由到您想要的页面就足够了,而不需要这种不必要的往返
routes.MapRoute(
name: "homepage",
url: "home/index",
defaults: new { controller = "Home", action = "Index", type = "promotion" }
);
routes.MapRoute(
name: "default",
url: "/",
defaults: new { controller = "Home", action = "Index", type = "promotion" }
);
// and your action...
[HttpGet]
public ActionResult Index(string type)
{
return View();
}
可能重复的@MusiclovingDianGirl你看清楚我的问题了吗?@MusiclovingDianGirl没有重复。请再说一遍,这能回答你的问题吗?很抱歉,当我调用
/?type=promotion
时,url实际上以“/”开头,正如您在上面看到的。我不知道url:“
”。那就是帮助
routes.MapRoute(
name: "homepage",
url: "home/index",
defaults: new { controller = "Home", action = "Index", type = "promotion" }
);
routes.MapRoute(
name: "default",
url: "/",
defaults: new { controller = "Home", action = "Index", type = "promotion" }
);
// and your action...
[HttpGet]
public ActionResult Index(string type)
{
return View();
}