Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.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# 在MVC3中定义自定义路由_C#_Asp.net Mvc 3_Asp.net Mvc Routing - Fatal编程技术网

C# 在MVC3中定义自定义路由

C# 在MVC3中定义自定义路由,c#,asp.net-mvc-3,asp.net-mvc-routing,C#,Asp.net Mvc 3,Asp.net Mvc Routing,我有一个MVC3应用程序,我想在其中修改路由,如下所示: public class DealsController : Controller { public ActionResult View() { return View(); } [Authorize] [HttpPost] public ActionResult Add(DealViewModel newDeal) { // Code to add

我有一个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时,我想将url重写为www.doamin.com/unsecure/deals/view。因此,任何不具有Authorize属性的路由都需要通过添加单词unsecure来修改


注意:我的应用程序中有多个控制器,因此我正在寻找一种能够以通用方式处理此问题的解决方案。

映射到一个DealsController的路由,并使用RedirectToAction(如果允许从该url执行控制器)。

请使用RedirectToAction

例如:

return RedirectToAction( new RouteValueDictionary( 
    new { controller = "unsecure/deals", action = "view" } ) );

如果需要自定义路线,只需执行以下操作:

routes.MapRoute(
            "unsecure", // Route name
            "unsecure/{controller}/{action}/{id}"
        );
确保在默认地图之前添加此


这应该行得通。我没有测试它。

使用两个单独的控制器:

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 }
);

你为什么不使用iis7.0的url呢?有什么帮助吗?或者放弃这个问题?我不想基于什么是安全/不安全操作分割我的控制器。我不想你基于什么是安全/不安全操作分割你的URL。为什么这是必要的?