开发者

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

开发者 https://www.devze.com 2023-03-14 08:01 出处:网络
I have created a Helper method in MVC3 Razor project as(Helpers.cshtml) @helper Link(string fileName, UrlHelper url){

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 开发者_Python百科inside my ContentPlaceHolder

Do we have a solution for this.


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

精彩评论

暂无评论...
验证码 换一张
取 消