How to get the URL generated from Scripts.Render(“~/bundles/someScript”)? - asp.net

When I do Scripts.Render(“~/bundles/someScript”), it may be translated into something like
<script src="/someURL/bundles/someScript?v=asdY9xOxSVHQfe4hgu-iqLwv6wr6rbpT3YvcuODIqQ1"></script>
My question is, how can I get the value that is generated this value /someURL/bundles/someScript?v=asdY9xOxSVHQfe4hgu-iqLwv6wr6rbpT3YvcuODIqQ1 and store it into some variable ?

I managed to find out the solution, is to use <%: Scripts.Url("~/bundles/someScript") %>. This will produce the URL generated by Scripts.Render method.

Related

How can i get the values of parameters from query string in aspx file?

Previously i passed the parameters to my page in a query string. Now, how am i supposed to get them and use them in my page ?
I tried this code and it doesn't work :
<asp:Label runat="server" ID="ShowParameter"><%# Request.QueryString["IDProduct"] %></asp:Label>
You need to use <%= Request.QueryString["IDProduct"] %>. # is used for databinding.
It also goes without saying that you should also check to make sure the query string is not null and that it actually exists. It is also generally not a good idea to directly output a query string like this for a number of reasons.

how to bind data as routeValue in telerik ListView template

My template modified from MvcMusicStore tutroial:
<script type="text/x-kendo-tmpl" id="template">
<div class="product">
<a href="#Url.Action("Details", "Store", new {id = ${ProductSubCategoryId} })">
<img src="#Url.Content("${AlbumArtUrl}")" alt="${Title} image" />
</a>
<span><h3>${Title}</h3></span>
<p>${kendo.toString(Price, "c")}</p>
</div>
</script>
but there is error in Url.Action method: Unexpected symbol '$'.
Update 1:
And i can't use
#Model.First(d => d.ProductCategoryId.Equals(Convert.ToInt32("${ProductSubCategoryId}"))).ProductCategory.Name
but same code in
#Url.Content("${AlbumArtUrl}")
work fine.
new {id = ${ProductSubCategoryId} }
This is C# code for an anonymous object, and C# doesn't know anything about JS templating. Hence the error telling you it doesn't know what that $ is doing there. Usually you'd pass something from your View model, which is only available serverside:
new {id = Model.ProductSubCategoryId }
Instead of using a URL helper, you might be better off with just a string href="Store/Details/${ProductSubCategoryId}". That may not be exactly what you need, but I don't know enough about the routing and your template to know if this is what you intended, or if ProductSubCategoryId is actually a property of your model.
In regards to your updated examples:
Url.Content( works because that function takes the parameter as the page is being rendered on the server, and just spits out with the string "${AlbumArtUrl}" in the HTML pretty much as it is, and the HTML will contain the string "${AlbumArtUrl}" so that when the JS template is parsed later on the client, it can interpret that variable. So in this case, the C# function .Content( doesn't need to understand that template variable, because to it, it is just a string that it embeds in the HTML. I would recommend using F12 in Chrome to view the GET response in the network tab so you can see the HTML source as it was returned from the action, so you have a better idea in your mind of what exactly what is happening at each step of the process. You'll be able to see that in your HTML returned, there is no C# code like Url.Content, but you will see the javascript template stuff like "${AlbumArtUrl}" because those values aren't rendered on the server side.
On the other hand Convert.ToInt32("${ProductSubCategoryId}") fails, because this function expects the string it's being passed to be an integer, such as Convert.ToInt32("2134"). As far as ToInt32 is concerned, it says, ok, I see "something" is a string, now I will try to interpret the string as a number which means I expect it to contain some digits, such as "456457" but you gave me a bunch of letters and symbols which means nothing to me, I can't convert that into an integer. Again, this is C# function that is running on the server as it generates the page, and "${ProductSubCategoryId}" means nothing to C# as it is a javascript template variable.
You are mixing server-side code with client-side code. This is invalid C# code:
#Url.Content("${AlbumArtUrl}")
You cannot use helper to generate URLs this way. I suggest you to go the normal way without using the Url helper.
If you are determined to use Url.Action, then here is a way. It is a hack but you can do this:
<a href="#Url.Action("Details", "Store")?id=${ProductSubCategoryId}">

ASP string format

i was used with php to use printf to build my strings, but i cannot find anything similar with asp and i end up writing crap like:
WrapTag="<"&Tag&">"&Text&" </"&Tag&">"
instead i would liek to write something more readable, like:
WrapTag=String.Format("<{0}>{1}</{2}>",Tag,Text,Tag)
as it was shown with this url:
http://idunno.org/archive/2004/07/14/122.aspx but it is not working.
Can anyone help me on that?
The code that you show is correct.
You don't have to send in the tag name twice, you can use the same value twice in the format:
WrapTag = String.Format("<{0}>{1}</{0}>",Tag,Text)

Get querystring from URLReferrer

I am trying to get the QueryString value like this Request.QueryString("SYSTEM") from a UrlReferrer. I see i can use this Request.UrlReferrer.Query() but it doesn't allow me to specify the exact parameter
I could parse the Query() value, but I want to know if it is possible to do something like this Request.UrlReferrer.QueryString("SYSTEM")
You could do
HttpUtility.ParseQueryString(Request.UrlReferrer.Query)["SYSTEM"]
That is c# in vb is is probably something like
HttpUtility.ParseQueryString(Request.UrlReferrer.Query())("SYSTEM")
In VB.NET, one way to get the value of "SYSTEM" from the UrlReferrer is:
HttpUtility.ParseQueryString(Request.UrlReferrer.Query).GetValues("SYSTEM")(0)

Using ASP <% .... %> Tags

I have been searching for this for quite a while and couldn't find a solution...
I have my aspx file and in it a asp:SqlDataSource, where I want to get values which are equal to the Request.QueryString["key"]. I have defined a parameter for it but I can't find the right syntax to set the value.
Currently it is looking like this:
<SelectParameters>
<asp:Parameter Name="courseID" DefaultValue="<%= Request.QueryString["course_name"]
</SelectParameters>
where I always get the error, it is not well formed. What is the correct syntax, and is there an article how you use this <%.. %> commands?
There's an MSDN page that goes over what each tag is and what it does. Probably using <%...%> is not correct, as that's just a code tag. You want <%=...%> or <%:...%> which actually write values to the page.
But! Actually, if I'm reading what your problem is correctly, you want neither of those. For a SqlDataSource to pull in a query string value, you want to add a <SelectParameters> tag to the datasource, then add a <QueryStringParameter> to that.
Edit:
Yep, looking at the edit you just made, you definitely want a QueryStringParameter.
You can't use <%= within high-level asp controls. That construct writes directly to the output buffer, whereas the controls need to be processed first. In other words, rather than processing your <%= before expanding the control, it must first expand the control before it can process your <%=. They are at different levels of abstraction.
To do what you want to accomplish, rather than a plain <asp:Parameter> use an <asp:QueryStringParameter>. This will allow you to set the key you want to use from the query string.

Resources