Routing in MVC doesn't always work correct - c#

My problem is as simple as the title.
I want to link to an action from a controller, but doesn't always work. I know what the problem is, but don't know how to solve it.
Imagine you're at the home page, the URL is then something like this: localhost:1234/. When I use the following URL in a link (<a>-tag) to go to the action configure, the href in that link-tag will then look like this: configure. But this doesn't work because it will send the user to localhost:1234/configure. Instead of this it should be localhost:1234/device/configure where device is the controller.
I could change the href in the link-tag to device/configure, but then it wouldn't work anymore when the user is redirected to the homepage. Because the url of the homepage is then localhost:1234/device/view (the default route, configured in RouteConfig.cs) and the link will send the user to localhost:1234/device/device/configure
I've already tried to use #Url.Action and #Html.ActionLink, but that doesn't work either.
Does anyone know how to make sure it will always send the user to the right URL?
Here is my RouteConfig if you need it.

Try this:
#Html.ActionLink("link text", "configure", "device")

Related

Having issue with using bookmark links with url routing

I have url routing setup on this site. I have a bookmark link in a nav that goes to a item in the home page like so:About . The problem is that, when someone entered the url incorrectly like site.com/contact/, instead of making the url site.com/default.aspx#about, it is making the url site.com/contact/default.aspx#about. All the other urls in the nav use the correct path. I know it has to do with the hashtag. Is there a way around it?
Instead of having
About
Try this:
<a href='<%= ResolveUrl("~/default.aspx#about") %>'>About</a>
This should always write the correct URL regardless.

How can I get the part of the URL that is getting changed whenever I clicked on some link?

My home page is :
http://localhost:8089/AFPWeb4.0/Project/ProjectList.aspx?StartPage=1
I have a link ProjectListInProjectGroups . When a user clicks on that link, my URL is changed to
http://localhost:8089/AFPWeb4.0/ProjectListInProjectGroups.aspx?
How can I get part of the URL that is changed ?
I need this value ProjectListInProjectGroups. I want to get this value in a ascx page.
Can someone help on this?
You can pass the current URL to the next page as a parameter and then you can find the difference between the two URLs using string operations.
If in your console you type window.location.pathname you would get the route after host name.
In console just type window.location and you can see what properties that has.

MSCRM Sitemap link to .htm page. How do I pass parameters?

I have a Site Map link that opens up a Web Resource .htm page. Everything works great except I want to pass some values to that page. I assumed I could pass them as a querystring since there is the Xrm.Page.context.getQueryStringParameters() method but my querystring seems to get stripped out.
<SubArea Icon="$webresource:my_webresourceicon.gif"Id="nav_my_webresourceid" Url="$webresource:my_webresourcepage.htm?xyz=123" Client="All" AvailableOffline="false" Title="My Web Resource Page" Description="My Web resource Description" Sku="All" PassParams="true"/>
I want to be able to access the parameter xyz from my_webresourcepage.htm as the page loads. What is the best way to go about passing this value?
Thanks
Unfortunately you can't pass xyz unless you modify the registry. Instead, you want to use the parameter "data".
http://msdn.microsoft.com/en-us/library/gg309536.aspx

How do I redirect to a website using MVC?

I am having a hard time figuring out how to redirect to an outside source.
in my code, I have
<%= Html.ActionLink("New Name Search", "Index") %>
which will allow me to navigate within the code.
how do I redirect to ...google for example?
google
The purpose of the ActionLink helper is to generate links that will direct the user to a controller action that you've defined.
If you want to navigate the user to an outside source you should just use a regular anchor tag.
Response.Redirect("http://google.com/");
(It's not really MVC-specific, by the way)
If you are redirecting from your controller (or action filter, etc.) you can use the RedirectResult as your ActionResult type:
RedirectResult("http://www.google.com");
This is essentially doing a Response.Redirect, but is the preferred way of sticking with ASP.NET MVC conventions.
If you are just creating a link inside a View, just use Click to go to Google.
"Redirecting" means many things.
If you just want to show a link that redirects the user to another URL you can use the anchor tag normally in you templates.
google
Now, if you want to redirect the user within the controller, call the Redirect Method.
Result("http://www.google.com");

Put current route values/url in form post

In my master page, I have a language dropdown menu. When the user selects a language from the dropdown, a submit sends the currently selected language to the "Translate" method in my controller. After which it should redirect to the url it was before the translation submit so it can show the exact same page, but now in the newly selected language.
How would I best go about this? Should I send the current url somehow in a hidden field? Or can I maybe send the current routevaluedictionary, so my translate method can redirectToRoute directly?
Or maybe there is an entirely better way?
--EDIT--
Because I want my bookmarks to include the site language too, all my exposed actions have a siteLanguage parameter too. If I could somehow efficiently show the user a bunch of regular (GET) links where the siteLanguage parameter is filled in with the relevant value, that would be even better. But as far as I know, there is no way to put links in a dropdown except with java maybe.
I have a similar situation, and I solved it slightly differently.
Because my master page had functionality in it, I created a new base controller class (that inherits from Controller) and all my real controllers inherit from my custom class.
Then, I implement OnActionExecuting in the base class to do some common work.
Then, in your masterpage, if you have a form like this, it will submit to the current URL with a GET request and add the language as a querystring parameter:
<form id="language" method="get" >
<select name="language">
<option value="en">English</option>
<option value="es">Spanish</option>
...
</select>
</form>
Use jQuery to wire it up to autosubmit, etc.
You could look for a language parameter in the querystring in your base controller class and set the flag that tells the real controller method which language to use. In the model, you directly go to the real controller to regenerate the page and avoid a redirect.
Note, this only works universally, if you are not already using querystring parameters.
If this doesn't work for you, you could also use your current method, but include the URL to be redirected to in a hidden field like this:
<%= Html.Hidden("redirect", Request.Url %>
public ActionResult Translate(string _lang){
switch(_lang){
case "English":
return View("English");
case: "French":
return View("French");
default:
return View("English");
}
I would personally do it like this
I would put the return url in the querystring, just like forms authentication does when it redirects to the login page.
Include the returnUrl in the routeValues when you do your Translate request.

Categories

Resources