ASP.NET, MVC, C# website pass a value though out the application - asp.net-mvc

This is a bit of a puzzle for me.
I need to capture the URI Query string passed to the Home page.
As a user travels to different pages on the web site, I have a partial view that needs that URI query string.
I need a dynamicly created link in the partial view, that equals the original call to the home page.
Example -
If the user goes to - http://www.mysite.com?mode=Joe , I need the dynamicly created link to equal - Http://www.mysite.com?mode=Joe
If the user goes to - http://www.mysite.com?mode=Tommy , I need the dynamicly created link to equal - Http://www.mysite.com?mode=Tommy
FYI - The partial view is used in the _Layout.cshtml file. the call inside - _Layout.cshtml looks like this -
#Html.Partial("MyPartial")
Thanks!

There are a number of ways to do this, but probably the simplest would be to save it to the session on your home page, and then access that session variable from your partial.
You will need to decide what to do if the session expires.
Another possible way would be to write it to a cookie on the home page request and then access the request cookie in your partial. Again, you'd need to decide on an approach for cookies disabled, or wiped out during browsing.
Alternatively, you could look at something like the approach being used in the link below to set a language across the site. Exact implementation would differ, but concept is the same:
Howto automatically add a specific value from current route to all generated links?

Related

How to categorize links without changing the URL

I'm re-building a site that's had lots of SEO work done to it. Currently built in ASP.NET web forms, I'm re-building in ASP.NET MVC.
All links have been "re-written" for SEO purposes. Examples:
- If you search for "foo", the search result URL will be "foo-stuff-for-sale", if you search for "bar", the URL would be "bar-stuff-for-sale" - you get it.
- The search results are "categorized" - i.e. categories are "widgets", "clocks", "clothing"...where the search term is contained in some portion of the products under each category
- If you click on a link from a category, the resulting search page would go back to the same search result, but without the category that was clicked on. i.e. - search for "foo", click on a link under "widgets", you'd go back to "foo-stuff-for-sale", but the "widgets category wouldn't show.
With Web Forms, this was being accomplished by a property in the .ascx page that was displaying the links and would be stored on the link itself somehow so that the link handler would be able to grab it and omit stuff from that category.
How do I accomplish the same thing using ASP.NET MVC and razor templates?
I've thought about:
Cookies - set the cookie using jQuery.cookie on click from a "data-category" attribute, read the cookie in the controller and then remove the cookie on display.
jQuery - setting something else on the query string onClick that the controller can read, but then doesn't show in the URL upon display.
UPDATE: Additional context: This was done in the old implementation via a session variable. I'm doing all I can to get rid of session state for this new implementation.
Thoughts? Ideas?

how to use resolve url in mvc controller

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

In an iframe, build url from parent page hostname and new params

I have two sites which communicate with each other. I'll call them mainsite.com and backend.com for this example.
mainsite.com is the site that people go to, and this loads an iframe which loads in backend.com, which is a Rails site that handles all of the login & signup stuff. At the end of the login process, backend.com generates a url which has an encrypted set of params which are used to log the user into mainsite.com.
The way that this is currently working is that backend.com controller generates a url like this "http://mainsite.com?auth=2347qweiuyqi" and saves it in a variable called #redirect_url, then (in the iframe) renders out some html which contains some javascript to redirect the parent page:
<script type="text/javascript">
$(function(){
window.top.location.href = "<%= #redirect_url %>";
});
</script>
This is working fine. However, we have a new situation where people can now access mainsite.com via a load of different urls (all subdomains on other sites, like mainsite.foo.com and mainsite.bar.com)
Now, when i redirect them to the auth url on mainsite, i want them to stay on whichever domain they're on. So, if they're on mainsite.foo.com, and loading backend.com in the iframe, then at the end of the login process i want to redirect the parent window to mainsite.foo.com?auth=2347qweiuyqi. Similarly, if they're on mainsite.bar.com they should be redirected to mainsite.bar.com?auth=2347qweiuyqi
My initial attempt at this was to change the javascript to get the domain name from the parent window, and build the redirect url by adding the params onto the end, then redirecting to this url. However, the iframe can't access the parent domain name because of cross-domain protection (ie the domains, "mainsite.foo.com", and "backend.com", don't match.
My next attempt was to have a javascript function on mainsite.com which takes the auth param and redirects to a url consisting of the current domain with that param added onto the end. But, i can't call a parent window function because of the same cross-domain restrictions.
How can I get past this? I have complete control over the content of both sites.
One solution which i know would work, but is a bit of hassle to set up, is to pass through the mainsite domain name through as a parameter in the src attribute of the iframe to backend.com, and then make sure that backend.com hangs on to that through the whole login/signup process, so that at the end of the process it can use it to build the whole url to redirect to. But, it feels like there should be a simpler way.
thanks, Max
EDIT - i've solved this by, when the first request comes through from mainsite.com to backend.com, saving the hostname of request.referer into the session, and then, later on when i come to do the redirect, looking it up from the session and adding the auth params on. This works but it feels a bit unsatisfactory so i'm interested in nicer solutions.

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