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"]" />
Related
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 :)
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.
I'd like to be able to turn rows of an html table into links to controllers.
I figured something like
<td onclick="<%:Html.ActionLink("", "Index", new {id=item.user_id}) %>">
I'm using MVC 2
Thanks.
<td onclick="window.location='<%:Url.Action("Index", new {id=item.user_id}) %>'">
The onclick attribute accepts some javascript code to execute. If you simply give it a URL, javascript doesn't know what to do with that.
In the snippet above, you're setting the window.location property to the desired URL. This causes the browser to go there.
EDIT: I also just realized that you were using the Html.ActionLink() method which actually generates an tag in your code. You'd be better off using the Url.Action() method, which actually generates a URL.
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');
<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