2016-09-29 76 views
0

我想返回一個RedirectResult的動作,但沒有Url.Action()。假設我有以下Controller返回ActionResult as 301

public class CustomerController : Controller 
{ 
    public ActionResult Index() 
    { 
     var url = Url.Action("CustomerAndProduct", new { customerId = 1, orderId = 2, productId = 3}); 
     return Redirect(url); 
    } 

    public ActionResult CustomerAndProduct(int customerId, int orderId, int productId) 
    { 
     // I want to get here... 
    } 
} 

有什麼辦法,我可以返回一個RedirectResult無需手動指定這兩個動作的名稱,並使用字符串的所有參數?原因是如果操作名稱或參數名稱發生更改,則維護這些字符串會很麻煩。然後它不會在編譯時被檢測到。

也許像這樣:

var url = Url.FromAction<CustomerController>(x => x.CustomerAndProduct(1, 2, 3)); 
return Result(url); 

非常喜歡一個可以與Futures library做:

Html.BeginForm<CustomerController>(x => x.CustomerAndProduct(1, 2, 3)) 

注意,我不想執行CustomerAndProduct。我只想將網址作爲301302返回。

回答

1

聽起來像你是要求某種類型的安全嗎?如果是這樣看看t4MVC https://github.com/T4MVC/T4MVC它會讓你寫

return RedirectToAction(MVC.Customer.CustomerAndProduct(1,2,3)); 
+0

歡迎來到100+俱樂部。 ;) 好例子。但是,我試圖將第三方降到最低,並且已經在使用Futures,這爲大多數幫助者提供了相同的內容。 – smoksnes