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
Related
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.
ASP.Net MVC has some really nice features for making sure that you have the correct URL for the route you want. So I can use the HtmlHelper class you get the correct URL for my views:-
#Html.RouteLink("Link Text", new {controller = "articles", action = "tag"})
Now this is great. However, I find myself in the situation that I want to know the URL but I am not writing it into a view. So my question is what is the best way to get this information in the controller? I have read various posts that show you how to sneakily create an instance of HtmlHelper but there must be a more straightforward way of doing this.
Thanks.
You can try the UrlHelper.RouteUrl. UrlHelper is accessible via the Url property on controller.
Controller has access to all information about the request, so Request object is where you can find the Url
Request.Url.ToString()
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
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
}
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?