I have the following error :'System.Web.HttpContext' does not contain a definition for 'GetOwinContext'
whenever I debug my project. this error is in the web API page, although it doesn't show an error in the default AccounController.cs and the nuget package is installed.what is missing?
using IdentitySample.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.Owin;
using Microsoft.Owin.Security;
using System.Web;
using System.Threading.Tasks;
using task.Models;
using System.Globalization;
public ApplicationUserManager UserManager
{
get
{
return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
}
private set
{
_userManager = value;
}
}
ok I found the solution since I already installed the nuget package all I have to do is the following:
return _userManager ?? HttpContext.**current**.GetOwinContext().GetUserManager<ApplicationUserManager>();
Related
I am hosting my angular application using DotNet Core 2.0 and here is my hosting configuration in program.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using System.Net;
namespace Samples.Web
{
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseKestrel(options=>{
options.Listen(IPAddress.Loopback,5050);
})
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.Build();
}
}
I overrided default port number 5000 of Kestrel using options.So http://localhost:5050 is working but how can I make a custom host url something like http://localhost:5050/samples working as well?
I tried UseUrls which didn't work. Thanks for help.
I am trying to inject ASP .Net MVC Session into a Controller by providing in interface for it using StructureMap. But StructureMap complaints while trying to do this as HttpContext.Current is not initialized at the time of registry initialization. I am sure there is a way around it I have yet to find. Please point me into the right direction.
Here is my code for more information:
DefaultRegistry.cs:
using System.Web;
using Company.O365Web.Infrastructure;
using StructureMap.Configuration.DSL;
using StructureMap.Graph;
namespace Company.O365Web.DependencyResolution
{
public class DefaultRegistry : Registry
{
#region Constructors and Destructors
public DefaultRegistry()
{
Scan(scan =>
{
scan.TheCallingAssembly();
scan.WithDefaultConventions();
scan.With(new ControllerConvention());
});
For<ISessionState>()
.Singleton()
.Use(new SessionStateProvider(new HttpSessionStateWrapper(HttpContext.Current.Session)));
}
#endregion
}
}
When testing my action in VS, everything is working fine.
After deploying, /site/controller responds as expected (403) while /site/controller/action responds with a 404.
Other api projects deployed from the same solution to the same root are working as expected.
Each project is configured as an application in IIS.
Can you give me a clue on how to proceed or what information to add here that would clarify the configuration?
Global.asax:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Formatting;
using System.Net.Http.Headers;
using System.Web;
using System.Web.Http;
using System.Web.Routing;
namespace ssa
{
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
GlobalConfiguration.Configuration.Formatters.JsonFormatter.MediaTypeMappings.Add(
new QueryStringMapping("type", "json", new MediaTypeHeaderValue("application/json")));
GlobalConfiguration.Configuration.Formatters.XmlFormatter.MediaTypeMappings.Add(
new QueryStringMapping("type", "xml", new MediaTypeHeaderValue("application/xml")));
}
}
}
WebApiConfig.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
namespace ssa
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
}
Iam not sure if you need to register all routes in your global.asax, but check this:
http://www.asp.net/mvc/tutorials/older-versions/controllers-and-routing/asp-net-mvc-routing-overview-cs
Check is your server configured to run targeted .net version it may be some differences between web api 1.0 and 2.0 at the server point of view.
I'm working on Asp.net MVC app, I'd like to use signalr.
Signalr to be hosted in IIS.
I added the signalr dlls and added the reference to it.
In Global.asax,
I put the code,
inside Application_Start()
" RouteTable.Routes.MapHubs();"
I've pasted the Global.asax,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
AuthConfig.RegisterAuth();
RouteTable.Routes.MapHubs();
}
}
When i run the app, say, "http://localhost:4432",
When I try "http://localhost:4432/signalr/hubs", I get exception as follows,
The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Requested URL: /signalr/hubs
Signalr version : I tried with V1.1.25 and 2.0
Someone direct me what I should do to get it corrected?
1.write a hub extends Hub
public class MessageHub : Hub
2.check web.config
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
I tried,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
RouteTable.Routes.MapHubs();
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
AuthConfig.RegisterAuth();
}
}
Put "RouteTable.Routes.MapHubs();" in the first line of Application_Start() method.
I am following the tutorial from this site here. Everything was going fine until I added the css and image folder. after adding these two things and running my project, I got the following error:
. I have two controller classes and one view class. If you guys want, I can share them with you. But if you also click on the link I provided, you can see the code there as well. Thanks for your time and help.
Here's my global.asax file:
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace MvcMovieStore
{
// Note: For instructions on enabling IIS6 or IIS7 classic mode,
// visit http://go.microsoft.com/?LinkId=9394801
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
// Use LocalDB for Entity Framework by default
Database.DefaultConnectionFactory = new SqlConnectionFactory(#"Data Source=(localdb)\v11.0; Integrated Security=True; MultipleActiveResultSets=True");
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
}
}
check the code-behind file, global.asax.cs, the namespace should be MvcMovieStore, and the class name should be MvcApplication, I guess the class name doesn't match.
THis is wiered but I did not do anything, I simply ran it again and it worked. Amazing.
I got the same error for no particular reason. Simply close and re-open Visual Studio has fixed my problem.
I found that changing the Namespace on the project, without refactoring that across the whole solution was my problem. Check your project Properties and see what your namespace is, ensure that lines up, across the board.