Pass relative URL ASP.NET MVC3 - asp.net-mvc

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
}

Related

How do I read Querystring variables in a Kooboo Module?

I need to be able to pass an ID to my Kooboo module.
I was hoping to add {id} to the page route and then somehow pass it to the module put I can't seem to find how.
I want the Querystring variable to be passed into the MVC Controller action.
Any Suggestions?
Managed to figure it out
Add a route to a controller in Module.Config
Use
#Url.ModuleUrl().Action("ActionName")
or
#Url.ModuleUrl().Action("ActionName", new { parameterName = 0 })"
To get an url to another action/controller within you module

Accessing route URLs in the controller in ASP.Net MVC

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()

ASP.Net MVC redirecttoaction not passing action name in url

I have a simple create action to receive post form data, save to db and redirect to list view.
The problem is, after redirecttoaction result excutes, the url on my browser lost the action section. Which it should be "http://{hotsname}/Product/List" but comes out as "http://{hotsname}/Product/".
Below is my code:
[HttpPost]
public ActionResult Create(VEmployee model, FormCollection fc)
{
var facility = FacilityFactory.GetEmployeeFacility();
var avatar = Request.Files["Avatar"].InputStream;
var newModel = facility.Save(model, avatar);
return RedirectToAction("List");
}
The page can correctly render list view content, but since some links in this view page use relative url, the functions are interrupted. I am now using return Redirect("/Employee/List") to force the url. But I just wonder why the action name is missing. I use MVC3 and .Net framwork 4.
I am new to ASP.Net MVC, thanks for help.
Your route table definitely says that "List" action is default, so when you redirect to it as RedirectToAction("List") - routing ommits the action because it is default.
Now if you remove the default value from your routes - RedirectToAction will produce a correct (for your case) Url, but you'll have to double check elsewhere that you are not relying on List being a default action.
Well, Chris,
If you get the right content on http://{hotsname}/Product/ then it seems that routing make that URL point to List either indirectly (using pattern like {controller}/{action}) and something wrong happens when resolving URL from route or {action} parameter is just set wth default value List. Both URLs can point to the same action but the routing engine somehow takes the route without explicit action name.
You should check:
Order in which you define your routes
How many routes can possibly lead to EmployeeController.List()
Which one of those routes has the most priority
Default values for your routes
Just make the route with explicit values: employee/list to point to your List action and make sure that is the route to select when generating links (it should be most specific route if possible).
It would be nice if you provide your routes mappings here.
but since some links in this view
page use relative url, the functions
are interrupted.
Why do you make it that way? Why not generate all the links through routing engine?
When using the overload RedirectToAction("Action") you need to be specifying an action that is in the same controller. Since you are calling an action in a different controller, you need to specify the action with the alternate overload e.g. RedirectToAction("List", "Employee").

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?

Is it possible to have Html.ActionLink add URL params automatically?

What I'd like is for all urls to contain a certain parameter without having to pass it to the views and add it to ActionLink through the routevalues param. I have a section in my site that I want to keep track of a "return param" for all links. It works fine for forms since Html.BeginForm sets the action to the exact current url already.
So, if the page I'm at is
/MyController/MyAction/300?ReturnTo=100
and I output
Html.ActionLink("Next Screen", "MyOtherAction")
I'd like to see
Next Screen
without having to do
Html.ActionLink("Next Screen", "MyOtherAction", new {ReturnTo = Model.ReturnTo})
You could write your own HtmlHelper extension that does this by calling Html.ActionLink, and combining the query strings. Use the ViewContext to get at the current query string.
Try this:
Html.ActionLink("Next Screen", "MyOtherAction", new {Model.ReturnTo})
The link will now appear the way you want it to be.

Resources