This might be really simple question and I though the <text> tags were the answer but they do not seem to be working on my part.
I simply want to display #azamsharp in my asp.net mvc application.
<text> #azamsharp </text>
gives me the following error:
The name 'azamsharp' does not exist in the current context
##azamsharp
Razor see the # and thinks you're escaping into code, so it looks for a variable azamsharp. ## tells razor you want to print an # symbol, not escape into code.
Try this instead:
##azamsharp
That should work
Related
I am currently making a site in ASP/MVC4 razor. I want to add an image, depending from the database value.
I have this:
<img src="../images/#movie.Language.jpg" />
But it doesn't work. How can I fix this?
Thanks in advance.
Try this:
<img src="../images/#(movie.Language).jpg" />
You have to realize that Razor is parsing the '.jpg' as part of the expression, but since Langauge doesn't contain a jpg property/field, it throws an error.
By using the parentheses, you instruct Razor to only evaluate movie.Language, and not '.jpg'.
See Tutorial.
I have Url.Content() snippets everywhere in my views. They work fine on pages with a simple URL, but not when the URL gets longer.
Here's a sample:
<img src="#Url.Content("~/Content/Images/logo.png")" id="logo">
This works fine when I'm on the homepage, or a page with URL localhost:8080/s/chocolate (which shows the result for the "chocolate" search.
But when I'm trying to add some refinements, e.g. localhost:8080/s/chocolate/b/lindt (which means filter the previous results to only ones from brand Lindt), it doesn't work anymore. In this case, Url.Content points to /s/chocolate/Content/Images/logo.png, which obviously fails.
It's as if Url.Content only goes 2 levels up the current location instead of using the real root of the web app. I guess it makes sense in the convention that URLs are in the form host/controller/action, but here I have a more complex URL scheme (I use URL rewriter module to match these URL fragments to action's parameters).
Is there any way to tell the helper to go to the real root, or any other solution to this problem?
(BTW, I'm using MVC 4)
EDIT:
As Felipe answered, I've just discovered that Url.Content is no longer necessary with MVC 4. That works for all "design" images with a constant path. However, I use a lot of images where the path is constructed partly with some data, e.g.
<img src="#Url.Content(string.Format("~/Content/Images/stores/{0}.png", cart.Store.Retailer.Id))"/>
I simply removed the Url.content, as such:
<img src="#string.Format("~/Content/Images/stores/{0}.png", Model.PreferedCart.Store.Retailer.Id)"/>
When rendered, this gives the following src: ~/Content/Images/stores_v2/Fr_SimplyMarket.png. The ~ being still here, the image is not found. How can I fix that?
In ASP.NET MVC 4+, you do not need to use Url.Content in attribute values. Use the ~/ syntax directly and let razor view engine 2 process the path for you. So your examples can be coded as follows:
<img src="~/Content/Images/logo.png" id="logo">
In the case you need to create a dynamic path, so, you have to use Url.Content, for sample:
#{
string imagePath = Url.Content("~/Content/Images/stores/" + Model.PreferedCart.Store.Retailer.Id + ".png");
}
<img src="#imagePath"/>
If it does not work, the reason is because you have problems with your URL rewriter.
In ASP.NET Core 5, It's working fine using below code in razor page.
#{
string logoPath = Url.Content("~/img.jpg");
}
<div style="background-image:url(#logoPath);">
What is the benefit to using Html.BeginForm?
We're having some issues related to html helpers and razor generator view testing and I'm struggling to see the benefit which would stop us going back to old skool form tags.
Has anyone got an argument for or against either?
by old skool i mean:
<form action="#Url.Action('Blah')">
The Html.BeginForm is useful because it generates the url using the routes defined in the Global.asax. (or you can extend it with your own code)
Using the old tag is neither worst or best in my opinion. You simply have to generate your url manually or using the Url helper. In the end the html in the page will be the same
<form ....>
html
</form>
Html.BeginForm also implements IDisposable, meaning the form must be closed properly. It's a minor thing, perhaps, but not closing Html.BeginForm produces a run-time error, where an unclosed <form> tag does not.
no there is no difference , the form tag just use the routing to generate the url , so if you use #Url.Action you are good to go
there is even books use that way a plain old tag and a url helper to generate the route
ASP.NET MVC Website Programming is an example
Edit
**
starting from Mvc 4 there is no difference , prior to Mvc 4 , Mvc 3 for example require the Html.BeginForm to make the javascript unobtrusive validation to work
Consider following:
$("#myform").attr({ action: "#Url.Action(MVC.Thing.Delete().AddRouteValue("id", myJsModel.Id )) });
I'm trying to set the action method of the form to a strongly typed T4MVC route. How do I insert a dynamic value from javascript into the route value?
I've seen the use of #: but I don't know how to insert it back into razor.
I don't think that using T4MVC here versus regular MVC syntax makes much difference when it comes to this issue.
When thing you might try is to generate a replaceable token on the server and do the replacement client side. e.g. something like
MVC.Thing.Delete().AddRouteValue("id", "SOMETOKEN")
And then take the generated path on the client and replace "SOMETOKEN" with myJsModel.Id.
Is there some magic existing code in MVC 2 to Html.Encode() strings and allow certain html markup, like paragraph marks and breaks? (coming from a Linq to SQL database field)
A horrible code example to achieve the effect:
Html.Encode(Model.fieldName).Replace("<br />", "<br />")
What would be really nice is to overload something and pass to it an array (or object) full of allowed html tags.
It's not a good idea to create your own whitelist based on regular expressions because you'll likely inadvertently open a security hole for XSS.
From Sanderson's book "Pro ASP.NET MVC3 Framework": "...The only viable mitigation is strict, whitelist-based filtering: use a library like the HTML Agility Pack to ensure the user-supplied markup contains only the tags that you explicitly allow."
Sanderson goes on to supply a link to a site that demonstrates a broad range of XSS techniques that you'd have to test for if you use the regex approach. Check out http://ha.ckers.org/xss.html
There is nothing built in to ASP.NET or MVC for this, but it's not that hard to write your own whitelist-based one with regular expressions and so on. Here's one that Jeff wrote, though it's pretty rough around the edges...
I can't think of anything off the bat but I guess you could write an extension method that allows you to add a paremeter/list of items to allow.
Html.Encode(Mode.fieldName, List<items> Myitems);
It could modify the allowable tags into < etc and then encodes the rest like normal.