asp.net-mvc – Razor MVC4 Url.Action无效

我在剃刀视图中有如下代码
<a href="@Url.Action("Details","Mycontr",new {id =16},Request.Url.Scheme)">

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

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}",action = "Section",id = UrlParameter.Optional }
 );

请建议.

我缩小了这个问题. Html.Raw导致问题.我有类似的代码

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

该变量包含生成的具有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...

有没有办法可以在不使用Html.Raw编码的情况下在变量中显示html脚本?
通过使用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\",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();

解决方法

我知道它有点晚了,但是当我在搜索某些东西时,我遇到了这个帖子.

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

<a href="@Url.Action("Details",Request.Url.Scheme)">

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

<a href='@Url.Action("Details",Request.Url.Scheme)'>

现在你href将是@ Url.Action(“详细信息”,“Mycontr”,新{id = 16},Request.Url.Scheme)

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....