gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 09:51:20 25 4
gpt4 key购买 nike

我在 Razor View 中有如下代码

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

当我双击此项目时,它会重定向到以下网址

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

但是,尽管预期是

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

下面是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 }
);

请提出建议。

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

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

该变量包含生成的具有 Ur.Action 的 html。如果我只是直接将 html 生成的代码放在没有 Html.Raw 的 razor View 中,它就可以正常工作。但是如果我取出 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...

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

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

                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();

最佳答案

我知道有点晚了,但我在寻找东西时看到了这篇文章。

您的代码问题出在您的 anchor 标记和双引号上。

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

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

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

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

关于asp.net-mvc - Razor MVC4 Url.Action 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15441085/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com