2011-03-08 27 views
2

我有以下途徑定義:如何擺脫ASP.NET MVC路線中的問號?

{theme}/{subtheme}/{contenttype}/{contentdetail}/Print 

當我使用
Url.Action ("PrintLayout","Page",new {contentUrlTitle = Model.ContentUrlTitle}

我得到以下鏈接:

/theme1/subtheme1/contenttype1/myfirstcontenturltitle?action=PrintLayout 

我想爲它是一個RESTful URL 。

/theme1/subtheme1/contenttype1/myfirstcontenturltitle/Print 

你知道我在這裏錯過了什麼嗎?

+2

你可以添加整個Routes.MapRoute代碼的代碼嗎? – 2011-03-08 10:59:42

回答

3

既然你還沒有發佈你的路線表(提示:張貼你的路線表),我將不得不猜測路線是什麼。如果你想要這個工作,路線必須是:

routes.MapRoute("contentDetail", 
    "{theme}/{subtheme}/{contenttype}/{contentdetail}/print" 
    new { controller = "Page", action = "PrintLayout", theme="", subtheme="", contenttype = ""  
    }); 

然後控制器:

public class PageController 
{ 
    public ActionResult PrintLayout(string theme, string subtheme, string contenttype, string contentdetail) 
    { 
     //do print stuff here 
    } 
} 
0

我想你可以試試這個:

Url.Action("PrintLayout/Print", "Page"); 

的事情是,當你使用一個字典來解析新參數的默認行爲是一個。

1

Jose3d,

  1. 一個重要的一點要注意的是,「控制器'和'action'參數在ASP.NET MVC中被視爲特殊參數。即使參數值部分中未明確指定這些參數,也會爲url匹配提供這些參數。

因此,以下語句:

Url.Action ("PrintLayout","Page",new {contentUrlTitle = Model.ContentUrlTitle} 

將提供用於路線匹配下列參數:

控制器= 「頁」,動作= 「PrintLayout」,contentUrlTitle =「的{值Model.ContentUrlTitle}「

正如你在這裏看到的,'controller'和'action'參數是由ASP.NET MVC隱式定義的。

  1. 另一件事,許多開發商不理解的ASP.NET MVC路由的是,路線匹配過程中,過量供應的任何參數將出現在網址爲「查詢字符串」

過多的參數是不會出現在網址中的參數。

對於您的情況,'action'參數不會出現在url中,因此它將被視爲'excess parameter',這就是爲什麼它顯示爲查詢字符串。

我的建議是:嘗試重新格式化您的網址,以便{action}是url網段的一部分。

我不明白的一件事是爲什麼'controller'參數不會顯示爲查詢字符串?也許提供更多細節可能會更有幫助。