Making a downloadable link in ASP.NET MVC - asp.net-mvc

Download Sample Form
Why is this link not working?

Files in App_Data are not served via HTTP you should place the XML file outside App_Data, eg. in /Content
Alternatively you must create an Action that returns the file contents via File action result, e.g.
public ActionResult SampleForm()
{
return File(Server.MapPath("~/App_Data/form.xml"));
}
And then link via:
<%= Html.ActionLink("Download Sample Form", "SampleForm", "MyController") %>

If you mean constructing it using the MVC routing engine and helpers, the method Url.Content is what you're looking for.

Related

Create Link to External Website

I am trying to create an external link to each individual listing's assigned website address. Using the following code: (The listing website is saved as google.com)
External Link
Takes me to:
localhost:3000/google.com
Is there any way to generate a link that would go to www.google.com instead of trying to find a route in my application.
The reason why it's bringing you to localhost:3000/google.com it's probably because the string you are passing to the href attribute is not a full qualified URL.
In fact, if in HTML you write
External Link
The string will be appended to the current page path. You should make sure that the input you pass always contains the schema. If the input never contains that, then you can assume it's http://
External Link
But this is not really a solution, just a workaround. You should definitely make sure that when you populate the website URL, you store a complete URL. In fact, some sites may require https.
In Rails you normally use the url_for and link_to helpers to generate an URL, but they will both cause the same issue unless you pass a full URL.
<%= link_to "External Link", "http://#{listing.website}" %>
Do it the Rails way:
<%= link_to 'External Link', "http://#{listing.website}" %>
You need to put in the protocol.
Google
Do you get it? =)
You can create link like this:
External Link

Deployed mvc app not including sitename in href links

I have an asp.net MVC web app using Kendo UI Grids with client templates. The client template specifies an href as follows:
.ClientTemplate("<a href='[controller]/[action]/[parameters]' />")
In a debug run, the link is correctly generated as follows:
http://localhost:[port]/[Controller]/[Action]?[parameters]
works beautifully.
However, in a deployed environment, the link generated is as follows:
http://[server]/[Controller]/[Action]?[parameters]
And that fails because it is missing the website name. So what I need is for it to generate the links as follows:
http://[server]/[WebSite]/[Controller]/[Action]?[parameters]
How do I need to specify my href links for this to work?
Don't hand-code your url's instead of use the UrlHelper which will take care of including the virtual directory into your urls:
.ClientTemplates(string.Format("<a href='{0}' />",
Url.Action("action", "contoller", new { param1, param2})))
If you want to use the client template value in the url you have to build the url dynamically because the Kendo UI template syntax won't inside the route value:
columns.Bound(m => m.Id)
.ClientTemplate(string.Format("<a href='{0}&Id=#= Id #'>Click me<a>",
Url.Action("action", "contoller", new { someNotTemplatedParam })));

the method problem and the difference between url and path

when i read the book "Aglie web development with rails 4th",i found the code
<%= button_to 'Add to Cart', line_items_path(:product_id => product) %>
what's the difference if i use "line_items_url" and the code doesn't has the method like :method=>:post,
why?
The path version produces relative urls such as /order/34/lines/ while the url version produces a full url such as http://localhost:3000/order/34/lines/.
The second form is often used in mailers when the user click a link in a mail client or in an external webmail.
In your site you won't notice any difference.
Moreover the :method=>:post option will produce a post request to your webserver. It will do that by adding a javascript code which will create a form on the fly, add parameters to it and do a submit call to send your browser to the requested page with a post method.
The _url helper generates an URL that includes the protocol and host
name. The _path helper generates only the path portion.

render usercontrol (cshtml) using #Html.Partial

I am getting my hands on MVC 3 and am confused that how do i use UserControls in my project.
I have created a usercontrol (cshtml) file called UserControl.cshtml and i am trying to render it Products.cshtml.
MyUserControl.cshtml resides in Shared folder.
In Products.cshtml:
<div>
#Html.Partial("MyUserControl.cshtml");
</div>
But i am getting this error. I dont know why is it trying to search .ascx file.:
The partial view 'MyUserControl.cshtml' was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Products/MyUserControl.cshtml.aspx
~/Views/Products/MyUserControl.cshtml.ascx
~/Views/Shared/MyUserControl.cshtml.aspx
~/Views/Shared/MyUserControl.cshtml.ascx
Is this the correct way to render usercontrol in mvc 3?
--Update--
This works.
#RenderPage("../Shared/MyUserControl.cshtml")
You do not need to specify the file extension, the view engine will handle that.
#Html.Partial("MyUserControl")
Phil haack has fantastic blog on the way to use Partial page.
http://haacked.com/archive/2009/11/18/aspnetmvc2-render-action.aspx

ASP.NET MVC - Is it possible to generate cross-application action links?

is it possible to generate cross-application action links using the HTML helper class?
I have the following (both are separate VS2008 projects):
http://mainwebsite/
http://mainwebsite/application
I would like to generate a link IN the /mainwebsite/application/ project TO /mainwebsite/. Is this possible? Or should I just hardcode a hyperlink?
addition:
My question also applies vice-versa. So generating a link IN /mainwebsite/ TO /mainwebsite/application/. I already managed to do like so, by simulating the application name as controller name:
<% =Html.ActionLink("ApplicationName","",new With {.Controller = "application" }) %>
Yes, you can do this. The HTML helper generates URLs using the routing system, which is completely ignorant of ASP.NET MVC and your application. If you have a route in your route table for the other application, then you can generate a URL for it using the HTML helper. There is no rule which states that your route table can only contain routes for the current application, although you obviously need to be careful about how you order the routes.

Resources