我有一个MVC3应用程序,我想在其中修改路由如下:
public class DealsController : Controller
{
public ActionResult View()
{
return View();
}
[Authorize]
[HttpPost]
public ActionResult Add(DealViewModel newDeal)
{
// Code to add the deal to the db
}
}
我想要做的是当用户请求 www.domain.com/deals/view 我想将网址重写为 www.doamin.com/unsecure/deals/图即可。因此,任何没有Authorize属性的路由都需要通过添加单词unsecure来修改。
注意:我的应用程序中有几个控制器,所以我正在寻找一种能够以通用方式处理这个问题的解决方案。
答案 0 :(得分:0)
映射到一个DealsController的路由,如果允许从这样的url执行控制器,则使用RedirectToAction。
答案 1 :(得分:0)
请使用RedirectToAction
示例:
return RedirectToAction( new RouteValueDictionary(
new { controller = "unsecure/deals", action = "view" } ) );
答案 2 :(得分:0)
如果您想要自定义路线,请执行以下操作:
routes.MapRoute(
"unsecure", // Route name
"unsecure/{controller}/{action}/{id}"
);
请务必在>> 之前添加
应该有用。我没有测试它。
答案 3 :(得分:0)
使用2个独立的控制器:
public class UnsecureDealsController : Controller
{
public ActionResult View()
{
return View();
}
}
public class SecureDealsController : Controller
{
[HttpPost]
[Authorize]
public ActionResult Add(DealViewModel newDeal)
{
// Code to add the deal to the db
}
public ActionResult View()
{
return RedirectToAction("View", "UnsecureDeals");
}
}
然后像这样路线:
routes.MapRoute(null,
"unsecure/deals/{action}/{id}",
new
{
controller = "UnsecureDeals",
action = "Index",
id = UrlParameter.Optional
});
routes.MapRoute(null,
"deals/{action}/{id}",
new
{
controller = "SecureDeals",
action = "Index",
id = UrlParameter.Optional
});
// the other routes come BEFORE the default route
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);