title: Razor MVC4 Url.Action not working
body: <p>I have a code as below in razor view</p>
  
  <pre><code>&lt;a href="@Url.Action("Details", "Mycontr", new {id =16}, Request.Url.Scheme)"&gt;
  </code></pre>
  
  <p>while I double click on this item it is redirected to below Url</p>
  
  <pre><code>http://localhost:49280/Mycontr/Section/@Url.Action(
  </code></pre>
  
  <p>But whereas the expected was</p>
  
  <pre><code>http://localhost:49280/Mycontr/Details/16
  </code></pre>
  
  <p>Below is RouteConfig</p>
  
  <pre><code>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 }
   );
  </code></pre>
  
  <p>Kindly suggest.</p>
  
  <p>I narrowed down the issue. Html.Raw is causing issue. I have the code like</p>
  
  <pre><code>@Html.Raw(HttpUtility.HtmlDecode(Model.sContent)) 
  </code></pre>
  
  <p>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 </p>
  
  <pre><code>&amp;lt;style type=&amp;#39;text/css&amp;#39;&amp;gt;&amp;lt;/style&amp;gt;&amp;lt;center&amp;gt;&amp;lt;div style=&amp;quot;width:460px;&amp;quot;&amp;gt;&amp;lt;div style=&amp;quot;width: 460px; float: left;&amp;quot;&amp;gt;&amp;lt;div s...
  </code></pre>
  
  <p>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.</p>
  
  <p>Below is the latest code that I have been struggling to get it work. Please help.</p>
  
  <pre><code>                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();
  </code></pre>