how to use resolve url in mvc controller - asp.net-mvc

Can any body help me to resolve url (route url) in MVC
when i enter into the system it redirects me to Dashboard it works perfectly.
I have user defined menu whose routing value stored in database as follows
suppose i entered into the system it will redirect me to Dashboard page
suppose i want to redirect to usermaster page then url should be as follows
http://localhost:6782/Home/Index
but when i try to redirect to home index page the url should look like this
http://localhost:6782/Dashboard/~/Voucher/Create
i want to remove Dashboard/~
how plz let me know

If you're using IIS URL Rewriting within your MVC application, e.g. internally treating http://yoursubdomain.example.com/MyController/MyAction as http://hosted.example.com/yoursubdomain/MyController/MyAction, Url.Content() will generate a correct subdomain-relative link. ResolveUrl() will generate an incorrect link in this situation.
Source:Should I use Url.Content() or ResolveUrl() in my MVC views?

You should consider about #Url.Content("~\Action\").

You should not store the URL in the database as the URL may change according to the routing rules.
Try to store the controller name and action name in the database and in your code that builds the menu use #Url.Action and pass the controller and action retrieved from the database

Related

URL redirection JSF

I have my my app url: 127.0.0.1:8080/reader/read.xhtml
The read.xhtml is populated thorough database and has got various
links which are hard coded in database. (read.xhtml is actually
retrieved as String from DB.) for e.g. there are links
(<a href ="/write.xhtml>write</a>)
/write.xhtml
/upload.xhtml
as I cannot add the context when i click the link it directs me to
WWW://127.0.0.1:8080/write.xhtml or
HTTP://127.0.0.1:8080/upload.xhtml
Is there any way I can redirect the link to
HTTP://127.0.0.1:8080/reader/write.xhtml.
Can Prettyfaces handle this. If yes how?
You can simply add the context path to the links by rendering it in front of your links. Something like:
write

return url is not working on Live server vs development server

I have an MVC3 Razor view which has a link that requires user login and its html is like this:
<a href="Home/Login?returnUrl=/myprojname/Product/Index">
and then an action method Login in homecontroller and a view. Which posts values to Logon action method.
inside the post action method I check if returnurl is not null then redirect to that URL. On local system it returns to this address
http://localhost:1235/myProjname/Product/Index
and works fine. But when I upload it to server (IIS 7), It returns to
http://domainname/myProjname/Product/Index
and gives 404 because there is no 'myProjname' there, If I remove the 'myprojname', it works with this url on live server
http://domainname/Product/Index
but for that I need to change href like this
<a href="Home/Login?returnUrl=/Product/Index">
In that case, It doesn't work with local system with URL
http://localhost:1235/Product/Index
Please suggest solution
Are you serious?
Okay, we'll i'll play along.
The solution is to create links properly, not hard code them.
#Html.ActionLink("Login", "Home", new { returnUrl = Url.Action("Index", "Product") })
On a side note, you probably don't need to pass returnUrl to the action. Pull it back from the Request.UrlReferrer.AbsoluteUri in the action method itself.
Not meaning to sound rude, but I strongly suggest you head over to http://www.asp.net/mvc and have a read/watch before proceeding any further.
using Url.Action and Url.Content helper methods will generate URLs relative to the application root, and avoid these problems.
Also, #Href("~/") will generate the full path to the application root (from a view).

ASP.NET MVC 3 update URL when returning a view.

In my controller action I return a view and I also need to update the url in the browser
so if the request url is test.site.testsite.com I want to change it to search.site.testsite.com how can I do this.
Thanks
So I'm not positive about sub-domains but HttpContext.RewritePath() allows you to modify the URL. This should give you a start.
You can achieve the above by following,
In IIS host two WebSites (test.site.testsite.com and search.site.testsite.com) both pointing to same physical/virtual directory.
In the Controller action , you can use a filter attribute which will redirect all search request to search.site.com.

Using MVC Routes as Shortcodes

We have been trying to implement shortcodes on an ASP.NET MVC web app that allow users to uniquely invoke a given article/page using an assigned short code.
For e.g.: www.mysite.com/power would map to an actual URL: www.mysite.com/Power/Home/.
I have created various routes throughout the site that map these shortcodes to various actions and controllers within the application. From a shortcode/route point of view, everything is working great.
I, however, noticed a couple of interesting things. I have hyperlinks that I use Url.Action method to generate the URL pointing pages. Many of these pages also have short codes associated with them. For e.g.: I have a link that says:
Go to Power page
This is a page that also has the previously mentioned short-code assigned to it. When I use Url.Action, I ideally expect it to create a link as /Power/Home/Index or /Power/Home, but since I also have a route constraint mapped to it, it now generates the link as /power.
Is there a way I can just use the actual link URL when generating links? I only want short-codes when I am sending out emails etc. I want the site to generate actual URLs.
This may or may not be possible, but I wanted to see if there were any ideas out there that I could use.
Anup
Index and Home are likely defined in your route table as defaults for the Action and Controller element. When you generate the Url it wont include the defaults if they aren't needed.
You could write your own Action overload or helper, which would allow you to take more direct control of the generated URL or action link. You could approach it from two different ways: 1) a helper to generate short-code specific urls and links, and/or 2) a helper to generate the full url and/or link. If Url.Action is returning the short-code version due to your routing configuration, I'd think a good place to start would be the second option, creating a helper/extension method that will generate the full url for you.
Here's how I solved this:
Instead of naming a route with short code to point to the action url, I made the route point to a different Controller action which would then redirect to the actual route that I want it to.
For e.g.: Originally I had the code "power" defined in the route table such that it would point to www.mysite.com/Power/Home.
Now instead of pointing it to that action - Index, controller - Home, area - Power, I make it resolve to: action - Power, Controller - Home, Area - ShortCode.
In the controller now, I simply do a RedirectToAction("Index", "Home", new { Area = "Power" });
This ensures that the actual links to /Power/Home do not resolve to the shortcode "power".
This is a simple fix increased the work by a little bit, but works like a charm.

Html.Action link and Html.RouteLink

Even If the url is same can i go to different action using Html.RouteLink and Action Link.Like when i click on news link i will go to news details.The url of this page is http://localhost:1390/en-US/latestnews/125.Now if i select the ddl of language in the site header in this pagei need to
go to the home page of the site.The ddl (on change) will take the same url but this time it needs to go to action in the sane controller.
The URL will direct to the controller/action on the first routing entry that it matches regardless of how you generate it. I'm not sure exactly what you are trying to accomplish, but I suspect that what you need to do is use javascript to direct to a different url, perhaps generated with Html.RouteLink instead of Html.ActionLink based on the value of the drop down list when it's selected. If I'm misunderstanding what you are trying to do, please clarify.

Resources