I am writing a real-estate related ASP.NET MVC application. I would like to auto populate the the user's (detected) location (city, state) when the site loads and allow them to search for using a different location. Location search is available on all pages on the site and I would like to preserve the most recent location value across all requests. Right now I'm planning to use cookies to preserve this data across all requests so it can be populated in all my views (via a layout file) but wonder if there is a better way of doing so.
I'd like to hear any suggestions.
Thank you,
JP
you could either use static variables or session variables. To use session variables in MVC you use:
HttpContext.Current.Session["Location"] = "East St. Louis";
To recall in your view with Razor you use:
#Session["Location"].ToString()
Related
I'm pretty new to ASP.NET MVC and I just want ask of this scenario is possible and, if so, could anybody provide any resource links on how to implement it.
Say I have a site that can be accessed from www.mysite.com, can I also have the same site load up through www.mysite2com, www.mysite3.com and so on? effectively providing the ability to run multiple sites from a single code base?
The idea is to have the site content and style sheet change depending on site visited but keep the structure the same.
thank you very much for any help you can provide :)
Kris
Yes, this is possible
http://web.archive.org/web/20100119084358/http://just3ws.wordpress.com/2010/01/03/skinning-your-asp-net-mvc-application-based-on-your-sub-domain
This example uses subdomains of the same domain but nothing stops you from using the same logic and have different images/CSS/paths etc generated based on full HOST/domain name
Django has the Flatpages app, which lets site admins change content on specific pages without changing code. Flatpage content i stored in the database, sort of like in a CMS. Flatpages are typically used for about-pages and such.
Are there any good equivalents for ASP.Net MVC? I.e., a convenient way to manage page-content persisted to a database.
No.
Django seems closer to a CMS then "ASP.NET MVC" which is both a framework and just a general design pattern.
Have a look at http://http://cmsmvc.codeplex.com, it allows you to create pages, and manage content on the page.
The solution is still in early stages, but it could help you out.
I'm about to take a look at how to implement internationalisation for an ASP.NET MVC project. I'm looking at how to allow the user to change languages. My initial though is a dropdownlist containing each of the supported langauages. Whoever a few questions have come to mind:
How to store the list of supported languages? (e.g. just "en", "English"; "fr", "French" etc.) An xml file? .config files?
If I store this in a file I'll have to cache this (at startup I guess). So, what would be best, load the xml data into a list (somehow) and store this list in the System.Web.Cache? Application State?
How then to load this data into the view (for display in a dropdown)? Give the view direct access to the cache?
Just want to make sure I'm going in the right direction here...
Thank you.
First tell us please what you understand under "internationalization"?
a) You want to only have UI in several languages
b) You want to have the content in several languages
Have a look at this other question where I did some explanation on the topic: Advice on ASP.NET Multi-Lingual Strategy
To your questions:
I would advice storing a list of supported languages in DB where you can identify them by 2-character code. See ISO 3166-1 alpha-2 country codes.
If you're coming from the ASP.NET WebForms world, then the first thing to unlearn would be the statefulness and cache when you're dealing with ASP.NET MVC. You can store a simple single value in session or cookies that will indicate the currently active languages. The server side will generate a view in the required language based on this submitted values.
You instantiate a model in your controller to be passed to the view. In the model you can have a property - a list with available languages. The view can (and supposed to) consume the data to display from its model.
Regarding where you could store a cached version of languages. You can consider having somewhere a static list that will be initialized at the first request (like weak references). It will be shared then across all requests for a given application domain, meaning all the users of your web app will see the same values (until the domain gets recycled or the server restarted). If you were to use the server session, then the list would be created again for each new user session.
I have an ASP.NET MVC app and I want to add
to each page a list of members online. Actually add to Master page.
Members belong to groups so I want to
show all members in there groups.
So what is the best way of doing this??
Eg On session start add to a collection that is in cache object
and remove it on session end in Global.asax.
Is that a reliable way??
Traffic wont be enormous.
Malcolm
See How can I determine the number of users on an ASP.NET site (IIS)? And their info? The same approach can be used for WebForms as well as ASP.NET MVC.
Yes, you'll have to use the Application object to store it,
and you could do it in the Session Start/Session End events.
Add to the Application on Start, and remove on End.
What is the best and cleanest way to implement A-B testing in asp.net mvc? That is, when we make new changes to an asp.net mvc web site, we want to test the new html/css/js with a certain subset of visitors (defined on cookie, login id, etc) and then analyze some metrics (page response time, number of pages visited, $$$ in sales, etc) afterwards to measure the level of success of the changes.
I am looking for a clean way to implement a way of choosing what view (html/css/js, etc...) to render using asp.net mvc.
Check out FairlyCertain (http://www.fairtutor.com/fairlycertain/) when you get a chance. It's a .NET A/B library that you can pretty much just drop into your project and start writing tests.
Unlike the Javascript libraries from Google and VisualWebsiteOptimizer, everything happens on the server so you don't suffer any performance, user experience or SEO issues. I've been using it in my stuff for a while now and it works quite well.
There is an A/B testing framework specifically for ASP.NET MVC. This is an open source software I wrote myself when, just like you, didn't find a free tool which works nicely with ASP.NET MVC and doesn't require much setup.
Google Content Experiments? It's a Javascript-based solution that doesn't require anything from your backend.
You include Google's Javascript on your page
The script randomly substitutes elements on your page as defined by your A/B test
Google's site shows you a nice breakdown of the results...
If you are using the spark view engine, you could probably do it with a variation of the theme filter (http://sparkviewengine.com/documentation/viewlocations#Extendingfilepatternswithdescriptorfilters). For each new visitor to the site, determine if you want them to see the existing or new version of the site and set a cookie. Wire up a descriptor filter that looks for the presence of the cookie and modify the view location to look in the folder containing the modified views. If an alternative view exists, the Spark engine will automatically render it in place of the "normal" view, otherwise it will render the normal view.
If you are using the normal WFVE, then the simplest way to manage this would be to define a folder under Views where your view alternatives live. When you want to provide an alternative view, you place it in a location that matches its position within the normal Views folder but rooted at the alternatives folder e.g. to provide an alternative to Views/Users/login.aspx place your new view at Views/Alternative/Users/login.aspx.
With a convention in place for locating your alternative views, you can extend the WebFormViewEngine and overload CreatePartialView / CreateView to inspect some aspect of the ControllerContext to determine whether to render the default or overloaded view and alter the path as appropriate e.g. changing .../Views/Users/login.aspx to .../Views/Alternative/Users/login.aspx.
I suggest you use Display Modes to achieve A/B testing.
But Display Modes just support simple problems by default.
If you already implement Display Modes in some other scenario. You can consider DisplayModeMatrix (just google it). It helps you use Display Modes more efficiency.
https://www.nuget.org/packages/DisplayModeMatrix/
Wth Display Modes you can simply delete/rename views after A/B testing to clean up your project.
I think there isn't a ready to use solution for this and you will have to improvise.
Try to override your current functionality in well defined points without breaking it. Explicitly draw a border where your regular code and A-B testing code lives.
Inversion of control principle might help a lot here too (i.e. - controller factory could provide derived controller instead of original one). For views&partialviews - you could change viewengine so it would try to look for 'MyPartialViewAB.ascx' instead of 'MyPartialView.ascx'.
And it might be a good idea to take a look what performance counters are (in case you haven't).