8

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

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

But whereas the expected was

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

Below is 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(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

&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.

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();
Naga
  • 2,368
  • 3
  • 23
  • 33

2 Answers2

13

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

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

Vinnie
  • 1,053
  • 11
  • 31
  • Thats right a double quote within double quote is misleading javascript – Naga Aug 02 '14 at 14:32
  • 1
    It might be a bit confusing to read, but using all double quotes is fine here. The @ prompts razor to switch to c# mode and it remains in c# mode until the closing round bracket. The inner double quotes will all be parsed as c#, by the time it is returned to the browser it will only have one pair of double quotes, containing a URL. – thelem Mar 13 '18 at 12:47
-5

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

/Publication/Capsule/ + capId

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

Community
  • 1
  • 1
Naga
  • 2,368
  • 3
  • 23
  • 33
  • 10
    What if your site is not on the root level? For example, if the site is at `/some/folder/Home/Index`, a link to `/Publication/Capsule/4` would not lead to your site. – Kobi Mar 17 '13 at 08:23
  • I have seen that it triggered right Action method (Capsule). The razor view page(.cshtml) where the above link is configured is not at the root. The action is invoked correctly because I have this configured in RouteConfig. – Naga Mar 17 '13 at 13:23
  • @Kobi, you are right however in my case the content is in same server & hence above solution worked. – Naga Aug 02 '14 at 14:31
  • Agree this is not the answer to the issue. welcome this downvote. – Naga Sep 01 '16 at 13:36