2011-03-08 44 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'参数不会显示为查询字符串?也许提供更多细节可能会更有帮助。