Asp.net mvc 如何消除ASP.NET MVC路线中的问号?

Asp.net mvc 如何消除ASP.NET MVC路线中的问号?,asp.net-mvc,model-view-controller,Asp.net Mvc,Model View Controller,我定义了以下路线: {theme}/{subtheme}/{contenttype}/{contentdetail}/Print 当我使用 Url.Action(“打印布局”,“页面”,新建{contenturltTitle=Model.contenturltTitle} 我得到以下链接: /theme1/subtheme1/contenttype1/myfirstcontenturltitle?action=PrintLayout 我希望它是一个RESTful URL /theme1/s

我定义了以下路线:

{theme}/{subtheme}/{contenttype}/{contentdetail}/Print
当我使用
Url.Action(“打印布局”,“页面”,新建{contenturltTitle=Model.contenturltTitle}

我得到以下链接:

/theme1/subtheme1/contenttype1/myfirstcontenturltitle?action=PrintLayout 
我希望它是一个RESTful URL

/theme1/subtheme1/contenttype1/myfirstcontenturltitle/Print

你知道我遗漏了什么吗?

我想你可以试试这个:

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

问题是,当您使用字典解析新参数时,默认行为就是这样。

由于您尚未发布路由表(提示:发布路由表),我将不得不猜测路由是什么。如果您希望这样做,路由需要:

routes.MapRoute(“contentDetail”,
“{theme}/{subtheme}/{contenttype}/{contentdetail}/print”
新建{controller=“Page”,action=“printloayout”,theme=“”,subtheme=“”,contenttype=“”
});
然后您的控制器:

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

  • 需要注意的一点是,“控制器”和“操作”参数在ASP.NET MVC中被视为特殊参数。这些参数是为url匹配而提供的,即使它们可能没有在“参数值”部分中明确指定
  • 因此,声明如下:

    Url.Action ("PrintLayout","Page",new {contentUrlTitle = Model.ContentUrlTitle}
    
    将为路线匹配提供以下参数:

    controller=“Page”,action=“PrintLayout”,contentUrlTitle=“{Model.contentUrlTitle}的值”

    正如您在这里看到的,'controller'和'action'参数由ASP.NET MVC隐式定义

  • 关于ASP.NET MVC路由,许多开发人员不了解的另一件事是,在路由匹配过程中,任何提供过多的参数都会在url中显示为“查询字符串”
  • 多余的参数没有出现在url中

    对于您的情况,“action”参数不会出现在url中,因此它将被视为“多余参数”,这就是它显示为查询字符串的原因

    我的建议是:尝试重新格式化您的url,以便{action}是url段的一部分


    我不明白的一件事是,为什么“controller”参数不同时显示为查询字符串?也许提供更多详细信息会更有帮助。

    能否为整个路由添加代码。MapRoute代码?