How do I convert an HttpRequest into an HttpRequestBase object? - asp.net-mvc

My problem is the opposite of this:
How do I convert an HttpRequestBase into an HttpRequest object?
In my ASP.NET MVC application I have a method used by many controllers that receive an HttpRequestBase as argument.
Now I have to call that method from another method, that is not an action (it's an nhibernate interceptor). In this second method I could access HttpContext.Current.Request, that is a HttpRequest and I cannot cast it to HttpRequestBase (I thought it was possibile due to the naming ...).
Does someone know in what relationship are this classes and how can I solve my problem?
Thank you.

You'll want to wrap your HttpRequest in a HttpRequestWrapper:
var wrapper = new HttpRequestWrapper(httpRequest);
The HttpRequestWrapper inherits from HttpRequestBase.

This is an another solution which does not require to create a new instance:
var httpRequestBase = myHttpRequest.RequestContext.HttpContext.Request;

In my application I had calls coming from several different places that needed access to the HttpRequestBase. I created this set of extension methods to get and convert from several different Http types into a HttpRequestBase, then used HttpRequestBase as the base type for interface and class methods through the application when I needed access to the request.
public static class RequestExtensions
{
public static HttpRequestBase GetHttpRequestBase(this HttpContext httpContext)
{
if (httpContext == null)
{
throw new ArgumentException("Context is null.");
}
return httpContext.Request.ToHttpRequestBase();
}
public static HttpRequestBase GetHttpRequestBase(this HttpRequestMessage httpRequestMessage)
{
if (httpRequestMessage == null)
{
throw new ArgumentException("Request message is null.");
}
HttpContextWrapper context = null;
if (httpRequestMessage.Properties.ContainsKey("MS_HttpContext"))
{
context = httpRequestMessage.Properties["MS_HttpContext"] as HttpContextWrapper;
}
if (context == null)
{
throw new ArgumentException("Context is null.");
}
return context.Request;
}
public static HttpRequestBase GetHttpRequestBase(this HttpApplication httpApplication)
{
if (httpApplication == null)
{
throw new ArgumentException("Application is null.");
}
return httpApplication.Request.ToHttpRequestBase();
}
public static HttpRequestBase ToHttpRequestBase(this HttpRequest httpRequest)
{
if (httpRequest == null)
{
throw new ArgumentException("Request is null.");
}
return new HttpRequestWrapper(httpRequest);
}
}
I came across several SO answers that helped me build these extensions:
How do I convert an HttpRequestBase into an HttpRequest object?
Convert HttpRequestMessage to HttpRequest
Get the IP address of the remote host

I find the following extension methods useful:
public static HttpContextBase AsBase(this HttpContext context)
{
return new HttpContextWrapper(context);
}
public static HttpRequestBase AsBase(this HttpRequest context)
{
return new HttpRequestWrapper(context);
}
Usage:
HttpContext.Current.AsBase()
HttpContext.Current.Request.AsBase()

Related

Why ExecuteResult method without body in JsonResult class

In MVC, ActionResult is abstract class having ExecuteResult abstract method.
Of-course, ExecuteResult will be implemented where ActionResult will inherited.
JsonResult is inheriting ActionResult class. So as per the basic rule I expect the implementation of ExecuteResult method in JsonResult class.
See below images.
ActionResult class
JsonResult class
But here no body or concrete implementation of ExecuteResult method in JsonResult class. However the method is overrides but without the body or implementation.
So where is the actual implementation of this abstract method?
That is how your Visual Studio is behaving. We need parituclar settings turned on to see the built-in libraries source code in VS which i can't recall, but you can see the source code for it here which is :
public override void ExecuteResult(ControllerContext context) {
if (context == null) {
throw new ArgumentNullException("context");
}
if (JsonRequestBehavior == JsonRequestBehavior.DenyGet &&
String.Equals(context.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase)) {
throw new InvalidOperationException(MvcResources.JsonRequest_GetNotAllowed);
}
HttpResponseBase response = context.HttpContext.Response;
if (!String.IsNullOrEmpty(ContentType)) {
response.ContentType = ContentType;
}
else {
response.ContentType = "application/json";
}
if (ContentEncoding != null) {
response.ContentEncoding = ContentEncoding;
}
if (Data != null) {
JavaScriptSerializer serializer = new JavaScriptSerializer();
response.Write(serializer.Serialize(Data));
}
}
In visual studio, you cant see any of the implementation details of the built in classes by default. For that install this plugin in your VS

htmlHelper.ViewContext.HttpContext.Session is null

I am trying to use an HtmlHelper in an MVC project that I'm working on but am hung up on this problem. Why is Session null when I try to call the helper method in my nav/menu partial on page load? It works anywhere else I use it in the project but when I try and use it in my layout or menu partial I get this null reference exception.
public static class HtmlHelpers
{
public static ApplicationUser UserInformation(this HtmlHelper htmlHelper)
{
var httpContext = htmlHelper.ViewContext.HttpContext;
if (httpContext.Session["CurrentUser"] == null)
{
// Get user information
var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext());
var applicationUser = userManager.FindById(httpContext.user.Identity.GetUserId());
httpContext.Session["CurrentUser"] = applicationUser;
}
return (ApplicationUser) httpContext.Session["CurrentUser"];
}
}
Try System.Web.HttpContext.Current rather than htmlHelper.ViewContext.HttpContext.
Example :
var httpContext = System.Web.HttpContext.Current;
if (httpContext.Session["CurrentUser"] == null)
{
// Do somthing.
}
You can get more information about session in here.
OR
public static ApplicationUser UserInformation(this HtmlHelper htmlHelper,HttpContextBase httpContext)
{
if (httpContext.Session["CurrentUser"] == null)
{
}
}
You can get more information about session

System.ArgumentNullException: Value cannot be null - Umbraco HTTPContext on save and publish

source: https://gist.github.com/sniffdk/7600822
The following code is run by an activity outside of an http request, so i need to mock the http context.
I have mocked the http context like so:
public class GetUmbracoServiceMockedHttpContext : IGetUmbracoService
{
private UmbracoHelper umbracoHelper;
public T GetService<T>()
where T : IService
{
UmbracoContext context = UmbracoContext.Current;
if (context == null)
{
var dummyHttpContext = new HttpContextWrapper(new HttpContext(new SimpleWorkerRequest("blah.aspx", "", new StringWriter())));
context = UmbracoContext.EnsureContext(
dummyHttpContext,
ApplicationContext.Current,
new WebSecurity(dummyHttpContext, ApplicationContext.Current),
UmbracoConfig.For.UmbracoSettings(),
UrlProviderResolver.Current.Providers,
false);
}
var serviceTypeProperty = context.Application.Services
.GetType()
.GetProperties()
.SingleOrDefault(x => x.PropertyType == typeof(T));
if (serviceTypeProperty == null)
{
return default(T);
}
return (T)serviceTypeProperty
.GetValue(context.Application.Services);
}
}
I inject this IGetUmbracoService service into a controller and call:
service.GetService<IContentService>().SaveAndPublishWithStatus(item);
... The following error occurs.
System.ArgumentNullException: Value cannot be null. Parameter name:
httpContext at System.Web.HttpContextWrapper..ctor(HttpContext
httpContext) at
Umbraco.Web.SingletonHttpContextAccessor.get_Value() at
Umbraco.Web.RequestLifespanMessagesFactory.Get() at
Umbraco.Core.Services.ContentService.SaveAndPublishDo(IContent
content, Int32 userId, Boolean raiseEvents) at
Umbraco.Core.Services.ContentService.Umbraco.Core.Services.IContentServiceOperations.SaveAndPublish(IContent
content, Int32 userId, Boolean raiseEvents) at
Umbraco.Core.Services.ContentService.SaveAndPublishWithStatus(IContent
content, Int32 userId, Boolean raiseEvents)
How do i mock the http context without using the frowned upon HttpContext.Current = ...?
I assume the relevant issue comes from:
RequestLifespanMessagesFactory.cs
which in turn is calling an implementation of this:
SingletonHttpContextAccessor.cs
I did some work with Umbraco, running it from a console app and then using the Umbraco API to call into Umbraco.
I believe I based it on this project: https://github.com/sitereactor/umbraco-console-example
Might be useful.
Thanks user369142. This is what ended up working:
I also had to make sure that i was not raising any events on the SaveandPublish calls... as the HttpContext expects there to be messages registered in the context but we do not mock any... If you make sure raise events is false, it skips over the code that cares about that.
public class CustomSingletonHttpContextAccessor : IHttpContextAccessor
{
public HttpContextBase Value
{
get
{
HttpContext context = HttpContext.Current;
if (context == null)
{
context = new HttpContext(new HttpRequest(null, "http://mockurl.com", null), new HttpResponse(null));
}
return new HttpContextWrapper(context);
}
}
}
public class CustomRequestLifespanMessagesFactory : IEventMessagesFactory
{
private readonly IHttpContextAccessor _httpAccessor;
public CustomRequestLifespanMessagesFactory(IHttpContextAccessor httpAccessor)
{
if (httpAccessor == null)
{
throw new ArgumentNullException("httpAccessor");
}
_httpAccessor = httpAccessor;
}
public EventMessages Get()
{
if (_httpAccessor.Value.Items[typeof(CustomRequestLifespanMessagesFactory).Name] == null)
{
_httpAccessor.Value.Items[typeof(CustomRequestLifespanMessagesFactory).Name] = new EventMessages();
}
return (EventMessages)_httpAccessor.Value.Items[typeof(CustomRequestLifespanMessagesFactory).Name];
}
}
public class CustomBootManager : WebBootManager
{
public CustomBootManager(UmbracoApplicationBase umbracoApplication)
: base(umbracoApplication)
{
}
protected override ServiceContext CreateServiceContext(DatabaseContext dbContext, IDatabaseFactory dbFactory)
{
//use a request based messaging factory
var evtMsgs = new CustomRequestLifespanMessagesFactory(new CustomSingletonHttpContextAccessor());
return new ServiceContext(
new RepositoryFactory(ApplicationCache, ProfilingLogger.Logger, dbContext.SqlSyntax, UmbracoConfig.For.UmbracoSettings()),
new PetaPocoUnitOfWorkProvider(dbFactory),
new FileUnitOfWorkProvider(),
new PublishingStrategy(evtMsgs, ProfilingLogger.Logger),
ApplicationCache,
ProfilingLogger.Logger,
evtMsgs);
}
}
public class CustomUmbracoApplication : Umbraco.Web.UmbracoApplication
{
...
protected override IBootManager GetBootManager()
{
return new CustomBootManager(this);
}
...
}

Web API, Light Inject and Passing a Static Dictionary to the data layer

We have a multi-database solution and are passing the connection string to a factory function like so:
container.Register<IDbContextFactory>(
f => new DynamicDbContextFactory(ClientConfig.GetConnectionString()),
new PerScopeLifetime());
ClientConfig contains a static dictionary that gets populated on app start that maps a sub domain to a connection string. It seems that this approach is causing a memory leak (not 100% sure about this causing the leak but there is a leak).
public class ClientConfig
{
private static ConcurrentDictionary<string, string> ConnectionStringManager
{
get;
set;
}
// etc.
}
My question is in MVC what is the best way to hold a list of connection strings that can be easily looked up on each request in order to pass that down the chain.
Edit : The question was initially tagged with Autofac
With Autofac you don't have to use a dictionary and something like that to do what you want. You can use a custom parameter :
public class ConnectionStringParameter : Parameter
{
public override Boolean CanSupplyValue(ParameterInfo pi,
IComponentContext context,
out Func<Object> valueProvider)
{
valueProvider = null;
if (pi.ParameterType == typeof(String)
&& String.Equals(pi.Name, "connectionString",
StringComparison.OrdinalIgnoreCase))
{
valueProvider = () =>
{
// get connectionstring based on HttpContext.Current.Request.Url.Host
return String.Empty;
};
}
return valueProvider != null;
}
}
Then register your Parameter using a Module
public class ConnectionStringModule : Autofac.Module
{
protected override void AttachToComponentRegistration(
IComponentRegistry componentRegistry, IComponentRegistration registration)
{
registration.Preparing += registration_Preparing;
}
private void registration_Preparing(Object sender, PreparingEventArgs e)
{
Parameter[] parameters = new Parameter[] { new ConnectionStringParameter() };
e.Parameters = e.Parameters.Concat(parameters);
}
}
Module you have to register inside your container using
ContainerBuilder builder = new ContainerBuilder();
builder.RegisterModule(new ConnectionStringModule());
Each time Autofac have to resolve a parameter of type String named connectionString it will used the custom parameter and get your connectionstring based on what you want.
By the way this code sample use HttpContext.Current. In case of a multithreaded process it may return null. I don't recommend using HttpContext.Current for such things. You can use an intermediate class instead of accessing it, for example a IConnectionstringProvider interface.
public interface IConnectionstringProvider
{
String ConnectionString { get; }
}
public class ConnectionStringProvider : IConnectionstringProvider
{
public ConnectionStringProvider(Strong host)
{
// get connectionstring based on host
this._connectionString = String.Empty;
}
private readonly String _connectionString;
public String ConnectionString
{
get { return this._connectionString; }
}
}
Inside your Parameter you will have to change the valueProvider by
valueProvider = () =>
{
return context.Resolve<IConnectionstringProvider>().ConnectionString;
};
And finally you will have to register your IConnectionstringProvider at the beginning of the request lifetimescope :
class Program
{
static void Main(string[] args)
{
ContainerBuilder builder = new ContainerBuilder();
builder.RegisterModule(new ConnectionStringModule());
IContainer container = builder.Build();
container.ChildLifetimeScopeBeginning += container_ChildLifetimeScopeBeginning;
}
private static void container_ChildLifetimeScopeBeginning(
Object sender, LifetimeScopeBeginningEventArgs e)
{
String host = HttpContext.Current.Request.Url.Host;
ContainerBuilder childLifetimeScopeBuilder = new ContainerBuilder();
childLifetimeScopeBuilder.RegisterInstance(new ConnectionStringProvider(host))
.As<IConnectionstringProvider>()
.SingleInstance();
childLifetimeScopeBuilder.Update(e.LifetimeScope.ComponentRegistry);
}
}
Of course there is many way to do it but you have the idea

ActionResult helper and different types resolver

Idea is to use same action methods for different types of results
I.E.
/category/details/?resultFormat=json
/category/details/?resultFormat=xml
So to have some kind of ActionResult helper that contains registered pairs of value resolvers
"json", JsonValueResolver
"xml", XmlResolver
etc...is there already solution for this or I have to think some kind of custom resolver?
Automapper has good solution for value resolving. Any ideas?
public class SmartResult : ActionResult
{
public override void ExecuteResult(ControllerContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
if (context.HttpContext.Request.QueryString["ResultFormat] == "json")
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
context.HttpContext.Response.Write(serializer.Serialize(this.Data));
} else if(context.HttpContext.Request.QueryString["ResultFormat] == "xml")
{
...serialize using xmlserializer
}else{
throw new InvalidOperationException();
}
}
public object Data { get; set; }
}

Resources