Razor Url.Action adding site folder path into Url - asp.net-mvc

I have an MVC project that lives on server in /root/folder1/, and a domain name pointing there.
So the url, www.site.com/Home is working fine.
However, all my #Html.ActionLinks and #Url.Action, etc. are rendering as www.site.com/folder1/Home
The links still work, but it is ugly, and I don't want the folder name to be known.
Anyone know why it's doing this and how to stop it?

You should be using something like this for your ActionLinks
#Html.ActionLink("Link Text", "ActionName", "ControllerName") 'NOTE YOU DO NOT NEED TO INCLUDE THE Controller part of the controller name. So for say AdminController you could simply set the controllerName in the ActionLink to `Admin`
You can also setup your ActionLinks like the below if you have to pass an Id or other value to the controller.
#Html.ActionLink("Link Text", "ActionName", "ControllerName", New With {.YourVariableName = "SomeValue"})
If you need to specify and HTML attribute for the control being rendered you can set those with one more overload.
#Html.ActionLink("Link Text", "ActionName", "ControllerName", New With {.YourVariableName = "SomeValue"}, New With {.htmlAttributeName = ""})
If you need to Specify the HTML attribute without a need to pass a value to the controller you can simply replace New With{.YourVariableName = "SomeValue"}withNothing`
Hope this works or at least points you in the right direction. A fresh MVC project out of the box has a Controllers folder. Which means server path looks something like root/controllerFolder/controller/action using the above works fine for me and only produces Url's that follow the standard {controller}/{action}/{id}. Say I have a need to call an Edit Action inside the AdminController which needs an ID so it knows what record I want to edit and I also want to add a class to this button so I can style it or anything else. I would do this
#Html.ActionLink("Edit", "Edit", "Admin", NEW WITH {.ID = #currItem.Id}, NEW WITH{.Class = "MyButtonClass"})
The URL that is generated will look like this
www.mysite.com/Admin/Edit/2
maybe this is a little more than you needed but your question appears to actually follow the standard {controller}/{action}/{id}. Just in-case I am missing something I figured I would try to at least point you in the right direction.

Related

Given a string that when appended to my domain would give me the appropriate url, how do you generate a link?

Given the following string:
/MyController/MyAction/4
How do I generate a link within another controller's action to link to the appropriate address?
If I do the following:
#Html.ActionLink("click", Url.Action(item.Link))
// where item.Link is where /MyController/MyAction/4 is stored
I get a link that resembles:
www.localhost.com/CurrentController/CurrentController/MyController/MyAction/4
I need to not have the "CurrentController" part (yes, there are two of them - I think that is because I am doing a #Html.ActionLink and an Url.Action).
How would I get my appropriate link?
If you already have the /MyController/MyAction/4 path you need stored in item.Link, could you just build the <a> tag yourself?
Link Text
Use the RouteUrl() method to achieve what you want. For more information, you can check this page as well.
I think what you are wanting is to link to another controller and action?
You need to do this;
#Html.ActionLink("Click", "ActionName", new {Controller = "ControllerName"})
Then you can add some HtmlAttributes onto that to;
#Html.ActionLink("Click", "ActionName", new {Controller = "ControllerName"}, new { #class= "className" })
Edit
If you are passing this string value in, then why not just use;
Click

Pass relative URL ASP.NET MVC3

I'm trying to pass a list of URL's with Id attributes from a controller to a view.
I can pass a <a href=...> link back but I don't think writing a 'localhost' absolute path is a clean way of approaching this. I cant pass an ActionLink back as it returns the full string. Is ther a simple solution to this problem? Thanks in advance.
Using this overload of the UrlHelper.Action() method and Request object you can get a complete URL including the route parameters such as IDs and the actual hostname of the application.
string url = Url.Action("action", "controller",
new System.Web.Routing.RouteValueDictionary(new { id = id }),
"http", Request.Url.Host);
UrlHelper is available in the controller via its Url property.
You can then pass such URL into your view.
It is also possible to use UrlHelper directly inside your view to create URLs for controller actions. Depends if you really need to create them inside the controller.
Edit in response to comments:
Wherever you need to place the URLs, this "URL builder" you are looking for is still the UrlHelper. You just need to pass it (or the generated URLs) where you need it, being it inside the controller, view or custom helper.
To get the links inside the unsorted list HTML structure you mention, you need to put anchors inside the list items like this:
<ul>
<li>Link</li>
...
</ul>
Then again you just need to get the URLs from somewhere and that would be from UrlHelper.
Simple and easy.
text
the route id = the parameter that is going to be inserted into your method.
eg.
function Details(int id) {
//id has the value of my_var_id
}

mvc route actionlink url use name instead of id

I think I already asked this but the solution didn't really made sense. Anyway, I have ActionLinks on my views like this:
foreach( var item in Model){
<%: Html.ActionLink(item.Name, "Details", new {id = item.Id}) %>
}
now, when it goes to my Action Details obviously this will be the url
/MyThings/Details/{id}
I was wondering if it's possible to show the name of that item instead of the id Using a slug? or modifying the global asax like so:
/MyThings/Details/CarKeys
The thing is the names need to be unique, is there a way I can modify the global asax to show the name instead of the id?
Any advice is much appreciated!
If this is solely for SEO purposes, just include both the ID and the Name. The name will be part of the URL but not used by your application. Just add another route that uses the template: {controller}/{action}/{id}/{name}
You'll also want to transform your name to make sure that it has only valid characters for urls. There are tons of articles about this on the net and it's pretty simple. Here's an example: http://chrismckee.co.uk/creating-url-slugs-permalinks-in-csharp/
Sure, you just have to change your routing rules. Look in your Global.asax.cs file, or in your area registration file, for something like this:
routes.MapRoute(..., "{controller}/{action}/{id}", ...);
... and change it to something like this:
routes.MapRoute(..., "{controller}/{action}/{name}", ...);
Then have your action take the name instead of the ID:
Html.ActionLink(item.Name, "Details", new {item.Name})

ASP.NET MVC create absolute url from c# code

How do i generate an absolute url from the c# code?
I want to generate a url like this: localhost/{controller}/{action}/{id}. Is there a way to do it in c# like how it can be done in the views?
It wont be generated inside the controller but inside a ViewModel.
string absUrl = Url.Action("Index", "Products", null, Request.Url.Scheme);
Just add Request.Url.Scheme. What this does is add a protocol to the url which forces it to generate an absolute URL.
Check out a similar question Using html actionlink and URL action from inside controller. Seems to be similar and reusable for your requirements.
If you don't want to "build" the url and just want the full path of the current page, this will do the trick
Context.Server.UrlEncode(Context.Request.Url.AbsoluteUri)
I know it's not as elegant as an Extension Method but thought of sharing it for educational purposes
As of latest update to MVC you can use below overload for Url.Action
string url=Url.Action("ActionName", "Controller",
new RouteValueDictionary(new { id= someid }),
//url param
HttpContext.Request.Url.Scheme,
HttpContext.Request.Url.Host);
which generates
http://localhost:port/Controller/ActionName?id=someid

add querystring parameter in URL

I am using asp.net mvc.
here I am not asking on controller logic but at view page.And I am not messing with default setting of url routing.
I have a view having some url like /controller/action?CID=2
In this view I want to put Link having above structure but with different controller.
The point is get the current url parameter and put hyperlink with same Querystring parameter.
<%=Html.ActionLink("linkText", "actionName",
"controllerName", new {CID = Request.QueryString["CID"]}, null) %>
Do you mean something like this?

Resources