2011-11-13 24 views
0

我創建一個網站,有路線非常相似所以,它們是:StackOverflow的一個類似路由重定向

posts/{id}/{title} 

我想對於任何給定的路線X

任何地方複製功能以/posts/{id}開頭,並且標題不正確,被重新路由到/posts/{id}/{title}

在控制器中這樣做似乎很尷尬,但在路由級別執行操作看起來更糟糕。

我想控制器會是要走的路。

public ActionResult Display(long id, string title) 
{ 
    // try and find post 
    // if post is null, error 

    // if post.title != title, permanent redirect to correct title 
    if (title != "correct-ness-ly") 
    { 
     return RedirectToActionPermanent("Display", new { id, title = "correct-ness-ly" }); 
    } 

    // permanent redirect if name doesn't match 
    return new ContentResult { Content = "display post-id : {0}, {1}".FormatWith(id, title) }; // found or not 
} 

有沒有一種「更乾淨」的方式來實現這一目標?

更新我剛剛意識到這讓我「失去」了可能存在的任何查詢字符串值。除了必須聲明地傳遞每個動作參數。

回答

0

您沒有指定您使用的是哪個版本的IIS。但對於7+,您可以使用redirect module

對於IIS 6,有幾個可以使用的第三方URL重定向模塊。

+0

我正在使用IIS 7.5,但我沒有真正訪問生產服務器上的IIS – bevacqua