-1

次のようにルートを変更する 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 という単語を追加して変更する必要があります。

注:アプリケーションにいくつかのコントローラーがあるため、これを一般的な方法で処理できるソリューションを探しています。

4

4 に答える 4

0

1つのDealsControllerへのルートにマップし、そのようなURLからコントローラーを実行できる場合はRedirectToActionを使用します。

于 2012-07-25T12:20:09.863 に答える
0

RedirectToActionを使用してください

例 :

return RedirectToAction( new RouteValueDictionary( 
    new { controller = "unsecure/deals", action = "view" } ) );
于 2012-07-25T12:21:21.577 に答える
0

カスタム ルートが必要な場合は、次のようにします。

routes.MapRoute(
            "unsecure", // Route name
            "unsecure/{controller}/{action}/{id}"
        );

これをデフォルトのマップのに必ず追加してください

それはうまくいくはずです。私はそれをテストしませんでした。

于 2012-07-25T12:27:22.090 に答える
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 }
);
于 2012-07-25T12:29:53.227 に答える