.net - Determine CurrentCulture And CurrentUICulture For An Arbitrary HttpContext?
Nov 9, 2010
Given an instance of an HttpContext object, is there a way to determine the CurrentCulture and/or CurrentUICulture for the thread it is executing on? Or more generally, is there a way to gain access to the current thread under which it is running?
View 1 Replies
Similar Messages:
Aug 23, 2010
I have some processes that run without an HttpContext in an ASP.NET MVC web application. This process needs to be able to determine the physical path to the Contents directory of the application for reading/writing data. But, since it is without an HttpContext, I don't get to use fancy things like Server.MapPath and such.
View 2 Replies
Mar 11, 2010
I was reading a resource that said:
CurrentUICulture must be set at the startup of a application.
For an ASP.NET web page, where do I set this property appropriately?
View 4 Replies
Jul 22, 2010
What is the best practice to set the CurrentThread.CurrentUICulture from a DrownDownList in a MasterPage?
I don't want to override InitializeCulture() in every page.
Could it be stored in a Session variable and set in a HttpModule or HttpHandler?
An additional problem i ran into is that if there are databound language-dependant controls on the page they will need to be databound after the language has changed.
View 2 Replies
Feb 16, 2011
have an asp.net mvc application where i want the user to be able to change language. I have provided a series of links with small flags on to let the user choose language. The target of all these links is my "dashboard" page, in which controller i have this code:
[HttpGet]
[Authorize]
public ViewResult Dashboard(string id)
{
if (!string.IsNullOrEmpty(id))
{
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(id);
}
}
The "Dashboard" page is displayed in the chosen language, as it should be. But when i navigate on through my website, the culture is changed back to english (default)... am i missing something? Shouldnt changing the CurrentUICulture change the entire application to the other language?
View 2 Replies
Apr 9, 2010
We're migrating an application to use IIS7 integrated mode. In library code that is designed to work either within the context of an HTTP request or not, we commonly have code like this:
if (HttpContext.Current != null &&
HttpContext.Current.Request != null) {
// do something with HttpContext.Current.Request
} else {
// do equivalent thing without HttpContext..
}
But in IIS7 integrated mode the check for HttpContext.Current.Request throws an exception whenever this code is called from Application_Start.
protected void Application_Start(object sender, EventArgs e)
{
SomeLibrary.DoSomethingWithHttpContextCurrentDetection();
}
Results in:
System.Web.HttpException: Request is not available in this context
How can I detect whether the request is really available without wrapping these calls in an exception handler and taking action based on whether an exception is generated or not.
Looking at HttpContext in Reflector I see it has an internal bool HideRequestResponse field but it's internal so I can only get to it with reflection and that's fragile. Is there a more official/approved way to determine if it's ok to call HttpContext.Request?
This blog post about the subject says not to use HttpContext, but how, in generic library code, can you determine if it's ok to use HttpContext?
http://mvolo.com/blogs/serverside/archive/2007/11/10/Integrated-mode-Request-is-not-available-in-this-context-in-Application_5F00_Start.aspx
I'm using the work-around mentioned there which is to use Application_BeginRequest and an initialized field to only initialize once as part of BeginRequest, but that has to be done in every calling application whereas I'd prefer to make the library code more robust and handle this situation regardless of where it's called from.
View 4 Replies
Aug 26, 2010
In my current application I want to implement ASP.Net localization with global resources. I have the problem, that after changing the CurrentThread.CurrentUICulture and CurrentThread.CurrentCulture and changing to another page, these values are overwritten by the browser default values.
I have a DropDownList that enables a selection between different languages. In the ItemChanged Event I store the culturename in the session, redirect to my defaultpage and use this code
protected override void InitializeCulture()
{
System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo("de-DE");
System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("de-DE");
base.InitializeCulture();
}
After switching to another contentpage, that does not override InitializeCulture I'm back to the default browser language. How can I make that persistent?
What options do I have? The following come to my mind:
On every *.aspx page I do implement InitializeCulture I create a new class that derives from System.Web.UI.Page and overrides the InitializeCulture Eventhandler. Every *.aspx page I use derives from it.
Isn't there a more "built-in" way? ASP.net offers such good localization support, so I guess there must be an easier/more efficient way to achieve my goal. Which one is there?
View 3 Replies
Oct 23, 2010
I am looking for some adivce about the possbility of doing the following while working whith MVC. Firstly sorry if the subject line is not very descriptive. I have put something together in ASP WebForms where by I can put a few core fields on my page that identify an object, e.g. a Name and ID... and then I can add arbritrary fields to the aspx page that are now saved and retrieved with the "owning object". These extra fields are stored in a seperate table on the database to the primary object fields. A short explanation of how this works:
1. I have my own version of TextBox (and other input controls) called r3d:TextBox (etc.) and this text box has XmlParent and XmlElement tags, this is done by way of an interface.
2. I have a r3dBase page that all my forms inherit from and this page check for the existstance of any r3d:TextBox (and other input controls) that have the XmlParen and XmlElement tag defiened.
3. When an iheriting form is saved the controls with the Xml tags defined are also saved, but I don't have to change any code in the code behind pages. The framework I have put in place takes care of this for me. The data is saved
4. When an inheriting page is loaded all controls with the XmlParent and XmlElement tags are populated with any data that has previously been saved for them that belong to the main object being displayed on the page.
This provides for a nice easy way for me to extend my forms with non core data when clients request new fields. It also means that different clients can request different new fields and I don't have to make any modifications to the core objects. I have another mechanism that determins what fields are seen by which clients.
Anyway I am completely new to MVC (not written a single line of code) so I have no idea as to whether this paradigm will lend itself to the above described mechanisms or not. Does any one have anythoughts about this, approaches I might try to achive the above to of functionality or perhaps completely different suggestions that would achive a similar end that might work well with MVC
View 2 Replies
Feb 14, 2011
I have a question about Html.ActionLink.
Imagine:
@Html.ActionLink(item.title, "Singlet", new { id = item.blog_id })
produces http://[url]/[controller]/Singlet/[id]
But what if I want to suffix some more arbitrary data at the end? For example:
http://[url]/[controller]/Singlet/[id]/#comments
To jump to the comments div. I know I can just make the string myself with something like:
@( new HtmlString(String.Format("<a href="Blog/Singlet/{0}/#comments">link to comments</a>", item.blog_id)) )
But I am hoping there is a cleaner way, perhaps with ActionLink?
View 2 Replies
Feb 11, 2010
We have a web portal product from which we customize portals from customers. We use the precompiled web app and create a virtual directory (vd) where the customization resides. In addition to this we do some changes web.config in the web app folder. We would obviously like to keep these customizations under TFS source control.
When I try to add the precompiled web app (which I don't want to add to source control), a warning tells me that the vds cannot be added. If I only add the folder that is referenced to by the vd, I lose the references to assemblies in the precompiled web app.
My questions are:
How do I structure a solution for adding IIS (sub application level) virtual directories and still retain the references to assemblies? Is it possible to add other directories/files from the web application level (like App_Theme, web.config etc.) to the solution?
Since we already use Visual Source Safe, we have established a tree structure for each customization project:
Project Root
|
|-Custom Sql
|
|-Custom Portal Files (which is added as a virtual directory)
|
|-Other Customizations
I could probably do a lot of this manually through the source control explorer, but I'd like to have everything done through a solution.
I've followed the instructions using this article: [URL], but this doesn't address the exact problem that I have. Oh, and we are currently using Visual Source Safe for portal customizaton, but are eager to make the move to TFS.
View 2 Replies
Feb 27, 2010
I want to create web application. I use SqlServer 2008 and asp.net (framework 3.5 sp1).
In my task, user can create database arbitrary structure. and i must write system that generate web site by template. is it possible?
View 1 Replies
Jan 14, 2010
My skills are failing me, and I know I've seen the code around for this but I can't find it. What's the quickest way to take any arbitrary URL, run it through your asp.net mvc routing system, and come out with a reference to a controller instance on the other end?For example, code execution is inside some arbitrary controller method. I want to do something like this:
...
string myURL = "[URL]";
RouteData fakeRouteData = new RouteData(Route???, IRouteHandler???)
RequestContext ctxt = new RequestContext(this.ControllerContext.HttpContext,
fakeRouteData);
ControllerFactory factory = ControllerBuilder.Current.GetControllerFactory();
Controller result = factory.CreateController(ctxt, controllername???)
I'm trying to get an instance of a controller just like the routing system does, regardless of where the code is executing. I'm unclear as to how to fit the pieces together at this point.
View 3 Replies
Nov 23, 2010
I have been experimenting with code that will clear all of the cookies in an HttpContext.Response.Initially, I used this:
DateTime cookieExpires = DateTime.Now.AddDays(-1);
for (int i = 0; i < HttpContext.Request.Cookies.Count; i++)
{
HttpContext.Response.Cookies.Add(
new HttpCookie(HttpContext.Request.Cookies[i].Name, null) { Expires = cookieExpires });
}
this will error with an OutOfMemoryException because the for loop never exits - each time you add a cookie to the Response, it also gets added to the `Request.
View 1 Replies
Nov 23, 2010
i'm using an example in which i can fake the session.It's for use in a unittest of a mvc controller.In the test i create a controller and then i do this:
FakeHttpContext httpctx = new FakeHttpContext(null,null,null,null,mSessionItems );
ControllerContext ctx2 = new ControllerContext(httpctx,new RouteData(), target);
here mSessionItems is my session and target is my instance of a controller,and indeed, when i'm in a controller reading this.ControllerContext.HttpContext.Session, i have a session, great!but.... i also read the session outside the controller, and there i use HttpContext.Current.Session, and that is null (or actualy, the HttpContext.Current is null).
View 1 Replies
Aug 28, 2010
Is it possible to say something to the effect of 'SomeClass.Out.WriteLine("hello world")' and have it actually show up for the browser to render? I ask, because I notice that the HtmlHelper BeginForm implements IDisposible. So at the end of the using block, a closing tag is written to the browser.I am not saying I would use this practice, as it seems like a bad idea, but I just want a better understanding of what is going on under the hood of C# ASP MVC.
View 1 Replies
Feb 22, 2011
If I were to respond to an http request with a plain text in PHP, I would do something like:
<?php
header('Content-Type: text/plain');
echo "This is plain text";
?>
How would I do the equivalent in ASP.NET?
View 4 Replies
Mar 30, 2010
How may I post arbitrary data to a REST WCF webservice? I mean, wihout use contracts.
Because my data is dynamic and I cannot have contracts, I want to get ride of the serialization overhead.
How should I declare my service and method?
View 1 Replies
Jun 4, 2010
I'm looking for a good way to add arbitrary properties to the objects in a strongly typed list, based on the principle that I shouldn't pass a DataTable from my business layer to my presentation layer.For example, I might have a Category class with the properties CategoryId and Title. On one page I would like to fetch a list of all categories (ie. List<Category>) together with the most expensive product in each category.A while ago, I would have just returned a DataTable with some additional columns in it with the product data in, but I'm trying not to do that -- it would be trivial to set up it's not good practice. One option is to add a MostExpensiveProduct property to my Category class, but I might want to display the most recently added product in another case, or the cheapest product, so I'd end up adding a lot of properties to cover all the options. This just doesn't feel right to me.Am I missing a trick here? What is the best way of doing this? Or should I just be returning a DataTable to which I can add as many columns as I need and not worry about it?
View 3 Replies
Feb 24, 2011
Is it possible to set the Inherits attribute of an ASPX Page directive to a class in an arbitrary assembly?
I need to modify an ASP.NET (1.1) application for which the source code was lost. In a new assembly (foo2.dll) I've created a replacement code-behind class that derives from the original class in the site's code-behind assembly (foo.dll). It seems easy enough to set the Inherits attribute of the Page directive to the new class name, but when I do that the web server gives me Could not load type 'Foo2.checkout2'. I am referencing the new assembly in the <assemblies> section of Web.config.
I don't see anything in the documentation to indicate that this scenario is unsupported, but I'm not certain that it is, either.
Original
<%@ Page Inherits="Foo.checkout" language="c#" Codebehind="checkout.aspx.cs" AutoEventWireup="false" %>
New
<%@ Page Inherits="Foo2.checkout2" CodeFile="checkout2.aspx.cs" CodeFileBaseClass="Foo.checkout" language="c#" Codebehind="checkout.aspx.cs" AutoEventWireup="false" %>
View 1 Replies
Aug 21, 2010
I want to write own control which can contain other. And I want to define content of the control in the .aspx file where the control is defined. I have written such control. But now I have issue with ViewState of inner controls of my control. The following samle illustrates the issue: I defined two asp:DropDownList ID="ddl1" and ID="ddl2" One of them is inside of my control and other is outside. When posback is occurred asp:DropDownList ID="ddl1 loses state and becomes empty. How to say ASP.net to store state of inner controls defined by this way?
MyControl.ascx:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="MyControl.ascx.cs" Inherits="DynamicControls.Controls.MyControl" %>
<div class="box"> [code]....
View 4 Replies
Apr 30, 2010
I am new in ASP.Net and C#.
I have used HttpContext.Current.Response.Redirect in a class file.
Is it right? or if any problem occur by using this?
View 2 Replies
Aug 5, 2010
I'm wondering how HttpContext.Current gets assigned a unique instance for every request considering it's a static object?
View 2 Replies
Aug 19, 2010
I have an ASP.NET site and I've been doing some work refactoring code to try to remove some long running processes (in the order of an hour) from the actual http Request by creating a BackgroundWorker and sending the work off to that to process. This was running fine on cutdown tests but when I applied the logic to the real code I found problems accessing Session variables from the code running in the Background Worker. It seems that the HttpContext object that was passed has a null session and if I ask for HttpContext.Current I get null back.
I'm assuming that this is because they are in a different thread and that the session and HttpContext.Current are both reliant on being in the same thread. Is there any way I can get access to the Session from the background worker or am I stuck with finding all the variables I need from session and putting them in an usable data structure and then putting them back in session (if appropriate) afterwards? It obviously complicates the refactor massively if I need to do this so I'd rather not.how I might do this other than BackgroundWorker processes
View 1 Replies
Feb 8, 2011
I'm trying to create a SessionManager class which I can use to manage sessions in my MVC applications. For that I'm thinking the best way of doing so is by creating a wrapper class for HttpContext which would then allow me to access HttpContext.Current.Session.
To be honest, I'm not really sure about the whole thing, I just feel it's the logical way of doing so. I also want to create an ISessionManager and ISession interfaces, and then implement them according to my application's needs. For my current project, and for now, I need a InProc session management, but I might need to store session data in MSSQL Server when we decide to expand and use a web farm or a garden. That's why I'm trying to build a sort of an extensible framework right from the start.
Final note, I will be using Microsoft Unity to inject the concrete SessionManager of choice. I believe that's a good way to maintain a certain level of abstraction.
View 1 Replies
Feb 2, 2011
I'm digging around with HttpContext and the OnInit event for a custom control, after realizing that a few checks are not responding in the desired manner.
Within the current context's items collection, I store an object (serialized) that I would like to interrogate once on every post back. The first time the control is rendered, will the items[] be null, it then gets populated somewhere down the line and the user clicks a button to post back data.
Now, within the button submit event, I set the items[key] to null to ensure that on the post back I don't have anything stored, but it always contains a value.
I'm assuming that the OnInit handler has a reference to the previous state, thus 'falsely' assuming that the items[] should contain a value. Is this something to do with the way OnInit works, or am I missing the bigger picture?
View 1 Replies