Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

<Link> in Helper method gives "Element link cannot be nested within element 'link'"

I have created a Helper method in MVC3 Razor project as(Helpers.cshtml)

   @helper Link(string fileName, UrlHelper url){
        <link href="@url.Content("~/Content/" + fileName)" 
              rel="stylesheet" type="text/css" />
   }

But i get a warning tooltip "Element link cannot be nested within element 'link'".

I found similar type of question on Asp.net Error when using link href inside my ContentPlaceHolder

Do we have a solution for this.

like image 731
Mangesh Pimpalkar Avatar asked Jun 21 '11 23:06

Mangesh Pimpalkar


1 Answers

Keep in mind that you should only have <link> tags in the <head> and not the <body>. The tooling is unable to confirm from where you'll call this helper, so it barks.

You can verify that this is what's going on by temporarily wrapping the helper in a valid skeleton of markup as below. The warning should go away. You can then confirm the negative case by changing link to some made-up tag like foo, or by removing the title, and watch those errors then appear. You should then be comfortable that as long as you only call your helper from inside <head> sections, your resulting link markup is ok.

<html>
<head>
    @helper Link(string fileName, UrlHelper url){
        <link href="@url.Content("~/Content/" + fileName)" rel="stylesheet" type="text/css" />
   }
   <title>Foo</title>
</head>
<body></body>
</html> 
like image 56
David Ruttka Avatar answered Oct 13 '22 01:10

David Ruttka