ASP.NET ResolveUrl returns only root - asp.net

I am wondering why ResolveUrl() function removes href attribute value and whatever I pass as a URL string ultimately leads to http://localhost:PORT. For example:
SomeText
resolves to
<a href>SomeText</a>
[EDIT] A workaround that helped me, but didn't solve the actual problem:
I didn't put the URL in ResolveURL function. I added the runat="server" instead. Like so:
SomeText

Use single-quotes like this:
<a href='<%= ResolveUrl(#"~/Home.aspx?param=1") %>'>SomeText</a>
Please mark as correct answer if this helped you :)

Related

use appsetting key in mvc view with string

I am trying to assign appsetting key value + string in MVC view.
I tried #System.Configuration.ConfigurationManager.AppSettings["myKey"].ToString()+"Custom/CustomerProfile.aspx", but did not worked.
Done
This is how I would do it, assuming you want to use that string in your href attribute of your link:
#{
var href = System.Configuration.ConfigurationManager.AppSettings["myKey"].ToString() + "Custom/CustomerProfile.aspx";
}
Done
But, it looks like you are not using Razor, because your link ends in .aspx, not .cshtml. Is that right?
Put parenthesis around the code to make it an explicit expression:
<a href="#(
System.Configuration.ConfigurationManager.AppSettings["myKey"].ToString()+"Custom/CustomerProfile.aspx"
)" id="doneLink" class="btn btn-primary">Done</a>
Below is a handy quick reference from Phil Haack:
http://haacked.com/archive/2011/01/06/razor-syntax-quick-reference.aspx
This works for me. What specific error are you getting?
<a href="#System.Configuration.ConfigurationManager.AppSettings["key"]" />

Image Hyperlink in ASP.NET - MVC 4

I try to create a project for the first time in asp.net (mvc4).
and what i try to do is to create a image which is a hyperlink to go to the index page.
i have search a lot of things and it shows very simple to do that.
but i can´t understand why it doesn´t work for me.
someone can give a hand?
Code:
<a href="<%= Url.Action("Index","Home")%><img src="~/Content/imagens/nav-arrow-back.png"/></a>
The Action is "Index" in the controller calls Home.
you miss a quote
<a href="<%=Url.Action("Index","Home")%>"> ...
^
about this quote you missed
For bad request, fix the whole <img> part
<img src="<%=Url.Content("~/Content/imagens/nav-arrow-back.png")%>"/>
First up, as previously noted you're missing a closing quote on that href. Second, MVC 4 doesn't use the <% %> syntax, at least not by default; it should be using Razor v2 which uses #, so your code should look like this:
<img src="~/Content/imagens/nav-arrow-back.png"/>
If you use the old syntax I assume it would try to handle the actual text <%= Url.Action("Index","Home")%> as a URL, which clearly won't work.

Why does ASP.Net rewrite relative paths for runat=server anchor controls?

I have my UserControls in a ~/Controls folder in my solution:
/Controls/TheControl.ascx
If specify the following:
<a id="theId" runat="server" href="./?pg=1">link text</a>
ASP.Net seems to want to rewrite the path to point to the absolute location. For example, If the control is on site.com/products/fish/cans.aspx the link href will be rewritten to read
<a id="munged_theId" href="../../Controls/?pg=1>link text</a>
Why does Asp.Net rewrite these control paths, and is there an elegant way to fix it?
I just want the anchor control to spit out exactly what I tell it to!!! Is that so hard?
EDIT:
I've basically done what Kelsey suggested. I knew I could do it this way, but I don't like adding markup in my code when I want something relatively simple. At least it solves the problem:
Aspx page:
<asp:PlaceHolder ID="ph" runat="server"></asp:PlaceHolder>
Code-behind:
var anchor = new HtmlGenericControl("a") { InnerText = "Previous" + " " + PageSize) };
anchor.Attributes["href"] = "?pg=" + (CurrentPage - 1);
anchor.Attributes["class"] = "prev button";
ph.Controls.Clear();
ph.Controls.Add(anchor);
As you can see by the amount of code needed for what is essentially supposed to be be a simple and light-weight anchor, it's not the most optimal solution. I know I could use a Literal but I figured this was cleaner as I'm adding more than one anchor.
I would be interesting in knowing WHY ASP.Net takes over and tries to fix my URL, though.
Why do you have runat="server" and no ID defined? Do you need to access it server side? If you remove the runat="server" everything will work as expected.
For more information regardinging how ASP.NET handles paths check out this MSDN article.
Edit: You can get around the problem then by using a Literal control and then outputing the raw <a href... to it.
Eg:
<asp:Literal ID="myLiteral" runat="server" />
myLiteral.Text = "link text";
Then you can set the visible property on the Literal however you want.
I know this is a bit of an old topic, but I was running into this problem as well and in the end went with a similar solution, but was able to save a few lines of code by doing this in the ascx:
<anchor id="myAnchor" runat="server" href="xxx">link text</anchor>
Then in the code behind, I referenced it using an HtmlGenericControl and can then do this:
myAnchor.TagName = "a";
// other properties set as needed
Anyway, I thought I'd post in case anyone else stumbles in here with the same issue.
Best bet is to make everything app root relative using the magic ~/ lead-in to the url. That tends to keep stuff straight.
There isn't a great answer to your question. ASP.NET is going to treat a relative path in a UserControl as relative to the path of the user control.
What you can do is in the code behind for your user control, set the HRef property of your anchor tag based on the Request.Path property. Then you can create URLs relative to the page.
Alternative is to use a literal like Kelsey was suggestion, or I would just try and map everything app relative with ~/ like Wyatt suggested.
Even a literal doesn't work using ICallBackEventHandler and RenderControl at least... I ended up hacking the tag back client-side :/ e.g in JQuery:
$('#munged_theId').attr('href', './?pg=1');

How to remove portion of url when using NavigateURL

I am using NavigateURL to dynamically pull in the url of products on a receipt page.
Here is the exact code:
<a class="blue13" href="<%#Eval("Product.NavigateUrl")%>"><%#Eval("Product.Name")%></a>
It is placing "/checkout/~/" in each of the url.
How can I remove or correct this?
Thanks!
The simplest thing would probably be to just call .Replace() and replace the unwanted part with a empty string. But it depends relay. Why is it there to begin with? Where is the data coming from?
I ended up switching from a regular href to an asp:HyperLink and it corrected the /~/ issue.
So, before I was using
<a class="blue13" href="<%#Eval("Product.NavigateUrl")%>" runat="server"><%#Eval("Product.Name")%></a><br/>
And I switched it to:
<asp:HyperLink CssClass="blue13" runat="server" NavigateUrl='<%#Eval("Product.NavigateUrl")%>' Text='<%#Eval("Product.Name")%>'></asp:HyperLink>
Which correct the issue.
Thanks.

String.Format in .aspx not showing rest of text after

<div visible="false" runat="server">Remove all items</div>
when I run this, it doesn't show the querystring portion, just the page.aspx. I don't see why the rest of that string after {0} is being cut off.
The problem with the question mark probably has something to do with using data binding (<%#...%>) instead of simple output (<%=...%>).
String.Format is overkill, as you only want to concatenate two strings:
<a href='<%=String.Concat(this.Page, ".aspx?removeItems=true")%>' >text</a>
Or simply putting the second string in the markup:
<a href='<%=this.Page%>.aspx?removeItems=true' >text</a>
Your string concatenation is unnecessary; have you tried this?
<a href='<%#string.Format("{0}.aspx?removeItems=true", this.Page)%>' >text</a>
Since it's ignoring the ?, try this:
<a href='<%#string.Format("{0}.aspx{1}removeItems=true", this.Page, "?")%>' >text</a>
The inline tag <%# is used for databinding, yet this.
Page isn't a databound property. Switch that out to <%=, which is equivalent to Response.Write & see if that works.
It's hackish, but sometimes that's what it takes in asp.net.
E.g. if you're using StringBuilder to create a javascript string at runtime and you try StringBuilder.AppendFormat, you can't have any other braces besides the Format braces. you can overcome that problem in a similar fashion to my answer using one string.format method and injecting "{" and "}".
The "?" issue may be a problem of codepage error handling withing databinding tags. For more information on this, see: http://support.microsoft.com/kb/893663

Resources