What am I doing wrong? How come <%= this %> isn't being interpreted as C#?
Here's the code :
And here is what it renders (notice the Firebug display):
What do you think is going on? MVC newb here. :(
And the static Site class:
(If you cannot see the screenshots on the page, view source and use the URLs from the <img> tags.)
<%: %> starts with .NET v4
For pre-v4 it's equivalent is <%= Html.Encode(...) %>
The problem was that I was using <%= %> (or even <%: %>) within a tag that had runat="sever".
Shouldn't that be <% %> or <%= %> for a shorthand of Response.Write?
Here's an MSDN article on Embedded Code Blocks.
This sometimes happens to me when embedding code inside of html attributes. I've never quite pinned down the exact cause but sometimes you can get around it by using single quotes rather than double.
Related
I'm trying to declare a string variable right after the body tag from the approach below and display the value for the name variable inside a div tag. But why can't we access the variables outside the declared scripting tag<%CODE%>?
In other words, if I declare a variable name somewhere in the HTML code
as <% string name="Stark"; %> and try to access it somewhere down in the code using the code below <div><%=name;%></div>...
Then it prompts me with
The <VARIABLE NAME> doesn't exist in current context
ASP.NET MVC
#{
string name = "Stark";
}
<div>
<h2>#name</h2>
</div>
ASP.NET WebForms
<% string name = "Stark"; %>
<div>
<h2><%=name %></h2>
</div>
<%= %> syntax prints an expression.
It doesn't take a full statement (name; makes no sense as a statement, and it doesn't make sense to print a statement).
Therefore, it must not have a semicolon.
I know this is a bit old of a topic but I want to answer it for anyone that stumbles upon this question:
For Webforms, ASP.NET separates variable contexts by the tags they are located in. Sometimes you need to set the variable within the same tag as the call (<%= %>) you are working with so that ASP.NET will compile it.
So...
<label> <!-- scope -->
<% %>
<%= %>
<\label>
Instead of...
<% %>
<label> <!-- scope -->
<%= %>
<\label>
The differences comes when you use an if/for/foreach code blocks in Webforms. The scope surrounding the HTML and C# allows for C# to become the focus instead of the HTML.
This is most likely a scoping issue based on how ASP.NET webforms handles variables in HTML.
If someone can confirm if the scoping ability has improved for razor, that would be awesome.
I have a template engine, which uses <% and %> for templates, but problem is ASP.NET WebForms think other about that.
In Razor I can escape # symbol just by doubling it — ##. How do it in Web Forms?
UPD: HTML escaping not helps — template engine don't want to use <% and %>, so site just show's them.
I may have found some answers here: http://www.sitepoint.com/forums/showthread.php?65377-escape-asp-tags
text = "<" & "% CODE %" & ">"
<% CODE %>
Temporary solution is creating server-side variables and use like <%= OPEN %> and <%= CLOSE %>.
Currently if I have this:
<div>
some dynamic data
</div>
I am using
<div>
<% Response.Write(get.SomeString()); %>
</div>
Obviously this works fine, but there definitely seems like there should be a shorthand for this.
You're looking for <%: get.SomeString() %>
The basic syntax is documented on MSDN:
Code render blocks define inline code or inline expressions that execute when the page is rendered. There are two styles of code render blocks: inline code and inline expressions. Use inline code to define self-contained lines or blocks of code. Use inline expressions as a shortcut for calling the Write method.
<% inline code %>
<%=inline expression %>
In your case, that would look like:
<div>
<%= get.SomeString() %>
</div>
Alternatively, while not noted in the MSDN documentation (but mentioned in the comments), in newer versions of ASP.NET, you can also use <%: … %> syntax to automatically escape any HTML in before writing it to the output. As Scott Guthrie explains, this is an important step in guarding against certain forms of attacks. Which form you should choose will depend on your exact use case.
I searched a way to include a file in a web application (like a menu, so I won't have to edit it on all pages when applying changes), but haven't found something as simple as
<?php include "Menu.html"; ?>
Can you please help?
Have you looked into Master Pages? They would certainly help you add the same layout across several pages.
Or perhaps you want a reusable User Control (that you write yourself)?
We don't use "include page" in asp.net, even though it is possible (with a different syntax of course). Instead, have a look at Master page concept.
MasterPages allow you to maintain a parent/child relationship between a master page which contains content that wraps around any number of child content pages.
Similarly, UserControls allow you to re-use whatever content you want on whatever page you want, whether it's a MasterPage or ContentPage:
<%# Page Language="C#" %>
<%# Register TagPrefix="uc" TagName="Spinner"
Src="~/Controls/Spinner.ascx" %>
<html>
<body>
<form runat="server">
<uc:Spinner id="Spinner1"
runat="server"
MinValue="1"
MaxValue="10" />
</form>
</body>
Methods (C#)
Executable code:
Page include
<!--#include file="a.aspx"-->
Execute independently inside a page
<% Server.Execute("a.aspx"); %>
Non-executable code:
<% Response.WriteFile("a.inc"); %>
I believe this is what you are looking for.
<!--#include file="wisdom.aspx"-->
I use C#.net
<% Response.WriteFile("YourPage.aspx"); %>
and this works real well for me!!
I also use your line,
<!--#include file="wisdom.aspx"-->
when I am in HTML mode.
What's better in ASP.NET MVC
<%= Html.LabelForModel() %>
or
<%: Html.LabelForModel() %>
? Why?
If it returns MvcHtmlString it won't matter; the <%: will know it is pre-escaped. I would therefore use <%: as then the reviewer doesn't need to think "is this escaped? or is this a raw field?".
Of course, that does depend on the code that creates the MvcHtmlString being sane...
Neither is "better". They both output the same exact markup.