javascript - How to use of @: in .cshtml - Stack Overflow

For example, I display some text in the view:@:<link href="Contentstylesheet.css" rel=&q

For example, I display some text in the view:

@:<link href="/Content/stylesheet.css" rel="stylesheet" type="text/css" /> 

@:<script src="/bundles/scripts" type="text/javascript"></script>

What is @: and when and how should I use it?

For example, I display some text in the view:

@:<link href="/Content/stylesheet.css" rel="stylesheet" type="text/css" /> 

@:<script src="/bundles/scripts" type="text/javascript"></script>

What is @: and when and how should I use it?

Share Improve this question edited Dec 15, 2015 at 19:37 Andy Hayden 377k109 gold badges639 silver badges541 bronze badges asked Dec 15, 2015 at 19:25 Rahul ModiRahul Modi 83813 silver badges21 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

This operator is useful in conjunction with other Razor server side operators when you want to output something as a literal text.

For example:

@if (model.Foo) {
    @:Some text to be written directly.
}

You can't just write this because it would produce an error:

@if (model.Foo) {
    Some text to be written directly.
}

So basically for your example where the output is already HTML tags you don't need this operator. But when you want to mix some server side Razor markup with an explicit output then you could use it. You could also use the special <text> tag to achieve the same effect:

@if (model.Foo) {
    <text>
    Some text to be written directly.
    </text>
}

This will trick the Razor parser to treat the text that you want to output to the HTML as literal and not consider it as server side code.

Another example. Let's suppose that you want to output some HTML tag that doesn't have a corresponding opening tag:

@if (model.Foo) {
    @:</div>
}

If you just wrote this:

@if (model.Foo) {
    </div>
}

then the Razor parser would have plained as it wouldn't know about an opening div tag.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745620207a4636472.html

相关推荐

  • javascript - How to use of @: in .cshtml - Stack Overflow

    For example, I display some text in the view:@:<link href="Contentstylesheet.css" rel=&q

    22天前
    60

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信