asp.net-mvc Razor MVC4 Url.Action 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15441085/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 02:39:23  来源:igfitidea点击:

Razor MVC4 Url.Action not working

asp.net-mvcrazorasp.net-mvc-routingurl.action

提问by Naga

I have a code as below in razor view

我在剃刀视图中有如下代码

<a href="@Url.Action("Details", "Mycontr", new {id =16}, Request.Url.Scheme)">

while I double click on this item it is redirected to below Url

当我双击这个项目时,它被重定向到 Url 下方

http://localhost:49280/Mycontr/Section/@Url.Action(

But whereas the expected was

但是,虽然预期是

http://localhost:49280/Mycontr/Details/16

Below is RouteConfig

下面是 RouteConfig

routes.MapRoute(
   name: "Capsule",
   url: "Mycontr/Details/{id}",
   defaults: new { controller = "Mycontr", action = "Details", id = UrlParameter.Optional }
 );
 routes.MapRoute(
   name: "Section",
   url: "Mycontr/Section/{id}",
   defaults: new { controller = "Mycontr", action = "Section", id = UrlParameter.Optional }
 );

Kindly suggest.

请建议。

I narrowed down the issue. Html.Raw is causing issue. I have the code like

我缩小了问题的范围。Html.Raw 导致问题。我有这样的代码

@Html.Raw(HttpUtility.HtmlDecode(Model.sContent)) 

the variable contains generated html that has Ur.Action. If I just place directly the html generated code in razor view without Html.Raw it is working fine. But if I take out Html.Raw the runtime generated html script that is displayed like

该变量包含生成的具有 Ur.Action 的 html。如果我只是将 html 生成的代码直接放在 razor 视图中而没有 Html.Raw 它工作正常。但是,如果我取出 Html.Raw 运行时生成的 html 脚本,该脚本显示如下

&lt;style type=&#39;text/css&#39;&gt;&lt;/style&gt;&lt;center&gt;&lt;div style=&quot;width:460px;&quot;&gt;&lt;div style=&quot;width: 460px; float: left;&quot;&gt;&lt;div s...

Is there a way that I can display html script in variable without using Html.Raw encoding? Half the issue got resolved by using HtmlString instead string variable for holding generated Html script, but the HtmlString couldn't decode @Url.Action syntax in the string.

有没有一种方法可以在不使用 Html.Raw 编码的情况下在变量中显示 html 脚本?通过使用 HtmlString 而不是字符串变量来保存生成的 Html 脚本,一半的问题得到解决,但 HtmlString 无法解码字符串中的 @Url.Action 语法。

Below is the latest code that I have been struggling to get it work. Please help.

下面是我一直在努力让它工作的最新代码。请帮忙。

                string templFile = string.Format("{0}\DivTemplate.htm", path);
            HtmlDocument divDoc = new HtmlDocument();
            StreamReader sRdr = new StreamReader(templFile, Encoding.UTF8);
            divDoc.Load(sRdr);
            XmlNodeList divs = regions.ChildNodes;
            IEnumerator enmrDivs  = divs.GetEnumerator();
            while (enmrDivs.MoveNext())
            {
                XmlNode node = (XmlNode)enmrDivs.Current;
                string divId = node["DivId"].InnerText;
                string capId = node["CapsuleId"].InnerText;
                HtmlString sUrlAct = new HtmlString("@Url.Action(\"Capsule\", \"Publication\", new { id=\""+capId+"\" }))");
                //string sUrlAct = "@Url.Action(\"Capsule\", \"Publication\", new { id=\""+capId+"\"})";
                string divFile = string.Format("{0}\{1}.htm", path, divId);

                HtmlDocument divRgnDoc = new HtmlDocument();
                StreamReader sR = new StreamReader(divFile, Encoding.UTF8);
                divRgnDoc.Load(sR);
                foreach (HtmlNode link in divRgnDoc.DocumentNode.SelectNodes("//a[@href]"))
                {
                    link.Attributes.RemoveAll();
                    link.Attributes.Add("href", sUrlAct.ToString());
                }
                HtmlNode divNode = divDoc.GetElementbyId(divId);
                divNode.AppendChild(divRgnDoc.DocumentNode.FirstChild.CloneNode(true));
                sR.Close();
            }

            sContent = new HtmlString (divDoc.DocumentNode.InnerHtml);
            sRdr.Close();

回答by Vinnie

I know its bit late but I came across this post when I am searching for something.

我知道它有点晚了,但我在搜索某些东西时遇到了这篇文章。

Here in your code problem is with your anchor tag and the double quotes.

在您的代码中,问题在于您的锚标记和双引号。

<a href="@Url.Action("Details", "Mycontr", new {id =16}, Request.Url.Scheme)">

You are technically telling the parser that you are referencing the hyperlink between "URL"(double quotes) that means href is @Url.Action(. You can do this way (use single quote to assign your href) and hope that works

您在技术上告诉解析器您正在引用“URL”(双引号)之间的超链接,这意味着 href 是@Url.Action(。您可以这样做(使用单引号分配您的 href)并希望有效

<a href='@Url.Action("Details", "Mycontr", new {id =16}, Request.Url.Scheme)'>

now you href will be @Url.Action("Details", "Mycontr", new {id =16}, Request.Url.Scheme)

现在你的 href 将是@Url.Action("Details", "Mycontr", new {id =16}, Request.Url.Scheme)

回答by Naga

I have solved the issue. I really dont need @Url.Action to the href. I have just replaced the code to logical path as below

我已经解决了这个问题。我真的不需要@Url.Action 到href。我刚刚将代码替换为逻辑路径,如下所示

/Publication/Capsule/ + capId

this linkhelped me solve the issue. Thanks to Jorge saving days of work :)

这个链接帮助我解决了这个问题。感谢 Jorge 节省了几天的工作时间:)