Url.Action返回浏览器中的当前链接

时间:2015-09-22 07:30:38

标签: c# asp.net-mvc asp.net-mvc-4 html-helper url.action

我目前正在页面/Customer/Edit/13244

当我在同一页面上使用@Url.Action("Edit", "Customer")时,它会返回/Customer/Edit/13244,但我希望它返回/Customer/Edit/

请告诉我如何解决这个问题。

2 个答案:

答案 0 :(得分:3)

这是MVC的一个“功能”,许多人发现它不自然,was previously reported as a bug

微软的官方回应:

  

最终,如果您希望最大限度地控制为URL生成的内容,可以考虑以下几个选项:

     
      
  1. 使用命名路由确保只使用您想要的路由来生成URL(这通常是一种很好的做法,尽管在这种特定情况下无效)
  2.   
  3. 明确指定所有路径参数 - 甚至是您想要为空的值。这是解决这一特定问题的一种方法。
  4.   
  5. 您可以使用Razor的〜/ syntax或调用Url.Content(“〜/ someurl”)来确保不会对您尝试的网址进行额外(或意外)处理,而不是使用路由来生成网址生成。
  6.   

实际上,当你试图重新定位动作方法名称时,这个bug只会让它变得丑陋。如果您使用Edit以外的其他操作方法名称,而id未跟随 { "functionId" : "2","subFunctionId" : "6","groupId" : "11","formId" : "","formName":"BladeInseption","submittedBy" : "200021669","createdDate" : "2015-08-06", "updatedBy" : "","updatedDate" : "","comments" : "","formStatusId" :"11","formStatus" :"Draft","formLanguage" : "English","isFormConfigured" : "N","formChange":"Yes", "sectionLevelChange":"Yes","isActive" : "Y","formVersionNo" : "1.0","formFooterDetails" : "","formHeaderDetails" : "","images" : [ {"imageId" : "","imageTempId" : "","imageTempUrl" : "","imageName" : "","imageUrl" : "","isDeleted" : "","imagesDesc" : ""} ], "imagesDescLevel" : "","sectionElements" : [{"sectionElement":[{"sectionId" : "","sectionTempId":"sectionId+DDMMHHSSSS","sectionName":"section1", "sectionChange":"Yes","stepLevelChange":"Yes","sectionLabel" : "","sectionOrder" : "1","outOfScopeSection" : "false", "punchListSection" : "false","images" : [{"imageId" : "","imageTempId" : "","imageTempUrl" : "","imageName" : "","imageUrl" : "","isDeleted" : "", "imagesDesc" : ""}],"imagesDescLevel" : "","isDeleted" : "","stepElements" : [{"stepElement":[{"stepId" : "","stepTempId":"stepId+DDMMHHSSSS", "stepName":"section1step1","stepLabel" : "","stepOrder" : "1","stepChange":"Yes","questionLevelChange":"Yes","images" : [{"imageId" : "", "imageTempId" : "","imageTempUrl" : "","imageName" : "","imageUrl" : "","isDeleted" : "","imagesDesc" : ""}],"imagesDescLevel" : "","isDeleted" : "", "questionAnswerElements" : [{"questionAnswerElement":[{"questionId" : "","questionClientUid" : "","questionDescription" : "step1question1", "questionAccessibility" : "","isPunchListQuestion" : "","questionChange":"Yes","questionOrder" : "1","isDeleted" : "","images" : [{ "imageId" : "","imageTempId" : "","imageTempUrl" : "","imageName" : "","imageUrl" : "","isDeleted" : "","imagesDesc" : ""}],"imagesDescLevel" : "", "answerId" : "","answerClientUid" : "","elements" :[{"element" :[{"elementId": "2","elementMapId" : "12","clientUid" : "","clientClass" : "","imageTempId" : "", "imageTempUrl" : "","elementType":"Question","elementOrder" : "1","elementArributuesProp": [{"attributeId" : "1","attributeName" : "","defaultValue" : ""}], "elementArributuesVal":[{"value1" : "item1"}],"rule" : [{"ruleId" : "1","ruleName" : "Mandatory","formula" : "i>a","formulaData" : "i>50","isDeleted" : "", ... } ,则此问题将会神奇地消失。

答案 1 :(得分:2)

您需要使用(假设您使用id = UrlParameter.Optional

的默认路线
@Url.Action("Edit", "Customer", new { id = "" })