C#.NET MVC路由别名 [英] C#.NET MVC Route Aliasing
问题描述
我一直在搜索并寻找旧的URL,就像我们以前在aspx页面中所做的那样,在其中您可能会有一个别名指向类似于www.domain.com/my-great-alias的页面指向www。 domain.com/alias.aspx。我想在MVC中做同样的事情,但无法弄清楚如何在路由表中做到这一点。 www.domain.com/my-great-alias会这样显示给最终用户,但指向www.domain.com/alias/2
I've been searching and searching for away to make old URL like we used to do in aspx pages where you could have an alias pointing to a page like www.domain.com/my-great-alias point to www.domain.com/alias.aspx. I want to do the same thing in MVC but can not figure out how to make this happen in the route table. Where www.domain.com/my-great-alias would show up to the end user as such but point to www.domain.com/alias/2
//路由器
routes.MapRouteLowercase(
"Alias",
"{id}",
new
{
controller = "alias",
action = "select",
id = UrlParameter.Optional
}
);
//别名控制器
public ActionResult Select()
{
return View("select");
}
//配方控制器
public ActionResult Select()
{
return View();
}
推荐答案
您应该可以利用路由配置和参数(只要它在同一个域中):
You should be able to do this utilizing route config and parameters (as long as it's in the same domain):
路由
routes.MapRoute(
name: "AliasRoute",
url: "{id}",
defaults: new { controller = "Alias" }
);
控制器
public class AliasController : Controller
{
public ActionResult Index(string id)
{
//DO SOME DATABASE STUFF HERE TO LOOKUP THE CORRESPONDIND CONTROLLER AND ACTION
var controllerAction = lookupControllerActionInDatabase(id);
return View(controllerAction.ViewName);
//OR
//DO CONDITIONAL CHECKS HERE AND RETURN THE APPROPRIATE VIEW
if (id == "my-great-alias") {
return View("Alias");
} else if (id == condition1) {
return View("viewForCondition1");
} else if (id == condition2) {
return View("viewForCondition2");
}
//AND SO ON...
}
}
这篇关于C#.NET MVC路由别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!