What is the namespace for Mapper.Map in asp.net mvc? - asp.net-mvc-4

I am working on ASP.NET MVC4 and i want to map my view model and database table object.
And for that i want to use Mapper.Map.But i don't have any idea about its Namespace.
Can anyone suggest me the namespace for that ?

That's not a namespace from MVC 4 directly. It's a namespace that belongs to AutoMapper.

Related

ASPNET MVC filter, WebAPI filter, ASPNET core MVC filter, are three different things?

Filters in ASP.NET confuse me. I have seen 3 kinds of filters:
MVC filter System.Web.Mvc.IAction​Filter
WebAPI filter System.Web.Http.Filters.IActionFilter
Core MVC filter Microsoft.AspNetCore.Mvc.Filters.IActionFilter
Is there a Core WebAPI filter?
I have a WebAPI filter which uses Dependency Injection via constructor parameters. It works well in my WebAPI application which uses Unity as IOC container.
container.Register<IMyService, MyService>();
container.Register<LogActionFilter>();
var filterInstance = container.Resolve<LogActionFilter>();
GlobalConfiguration.Configuration.Filters.Add(filterInstance);
The following attribute Log is defined as a simple attribute which only has three properties. The LogActionFilter above will check if an action has the Log attribute using GetCustomeAttribute<LogAttribute>().
[Log(Enabled=true, Level=2, Format="xxxx")]
public void MyAction()
{
}
Now I want to migrate it to ASP.NET CORE. Can I use DI for CORE filters?
I only find Microsoft.AspNetCore.Mvc.Filters.IActionFilter in ASPNET CORE. And it seems hard to use DI. Still don't know how to register such a filter.
I know there is ServiceFilter(typeof(XxxFilter)) but it's not good to pass paramters like [Log(Enabled=true, Level=2, Format="xxxx")]
Anyone has an example of CORE filter?
Thanks.
TL;DR: They are all pretty much the same thing.
System.Web.Mvc.IAction​Filter is an MVC action filter from ASP.NET MVC.
System.Web.Http.Filters.IActionFilter is an MVC action filter from ASP.NET MVC WebAPI.
Microsoft.AspNetCore.Mvc.Filters.IActionFilter is an MVC action filter from ASP.NET Core.
So the common theme is that they are all MVC action filters. That means that they run around the execution of an action within the context of an MVC framework.
The difference is just that they are used for different MVC frameworks.
ASP.NET MVC is the older MVC framework running on the .NET Framework. WebAPI is the framework that was developed specifically to create APIs for the web. It is generally similar to ASP.NET MVC but is still a separate entity.
With ASP.NET Core, the new and current open source MVC framework by Microsoft, the distinction between “MVC” and “WebAPI” was removed and instead you just have a single MVC framework included within ASP.NET Core. So you just have ASP.NET Core, and there you happen to use action filters around MVC actions.
There is very good documentation on MVC filters for ASP.NET Core. There is also a section explicitly about dependency injection in filters. You can use the ServiceFilter or TypeFilter to properly use dependency injection within your filters. ServiceFilter is used when you want to resolve the filter completely from the DI container, while you can use TypeFilter to also provide some parameters directly in the attribute. The example on TypeFilter ironically also uses a logging example.
That being said, logging is generally not the best use case for action filters since ASP.NET Core actually does log around the execution of actions by default.

Using Identity DbContext and DbContext in a class library in ASP.NET Core

I have a .NET Core 2.0 solution which contains a class library project and an ASP.NET MVC project. The MVC project reference the class library. Class library has all the entity classes and DbContext using EF Core. Everything was fine up to that.
Then I added ASP.NET Identity to the MVC project. It creates a separate IdentityDbContext to create the identity-related entities. I want relationships (foreign keys) between the Identity user entity and some of my other entities (I am using code first migrations). And I don't want to do migrations twice from both DbContextes. What is the correct approach to use here?
Combine your two contexts into one. In other words, just make your original context inherit from IdentityDbContext, instead of DbContext and dump the one the Identity scaffold created for you.

Is there a way to use a custom stores?

I'm new to ASP.NET Core and trying to build web api with Openiddict for security, what I'm looking for is a way to implement my own UserStore and other stores and use them ?
Is there an easy sample or example to follow ?
I tried to implement IUserStore and add it to IServiceCollection and used AddUserStore<MyUserStore>().
When I'm trying to execute identityUserManager.CreateAsync(user,..) I'm getting the following error.
The entity type
'Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityUserLogin'
requires a primary key to be defined
I think the user class I'm creating and because it's inheriting from IdentityUser which is in Microsoft.AspNetCore.Identity.EntityFrameworkCore, causing this problem, now I didn't find another IdentityUser in another name space two inherit from, isn't there any ?

How to access ASP Core HttpContext in ASP 4.5 class library project?

I have ASP 4.5 website with a dependency on the business logic project, which is a class library built using .Net v4.5. Recently to expand the development, I have planned to introduce additional website project which is ASP Core. For the same, I have added the dependency of the business logic project. The business logic currently evaluates the request, cookies and session related stuff through HttpContext.Current instance. Which isn't working when I am accessing the business logic through ASP Core website.
Access HttpContext.Current
I have gone through the above question, and can know that why HttpContext.Current appears as null when I access it through ASP Core. And the answer to it suggests to populate the reference type IHttpContextAccessor through dependency injection. Now, the problem is, this interface belongs to the library Microsoft.AspNet.Http, and I don't see a way to add this to my business logic project.
Someone please help me out to access HttpContext into my business logic project.
I had this exact same need. The way I solved it was to create my own interfaces that live in my class library and I use those to get access to the current HttpContext regardless of whether the library is running under a 4.5 http context or a MVC Core http context.
To explain further, both the ASP 4.5 framework and the ASP Core Framework have an HttpContext object with associated Request and Response objects but they are defined in different namespaces and neither framework knows about the other framework's namespace. So What I decided is that I needed my library code to have access to an IHttpContext that was defined in one of my namespaces. And that IHttpContext would use an IHttpRequest and IHttpResponse that were also defined in my library's namespace. And finally, that IHttpRequest uses ISession, IHeaders and ICookies that are defined in my namespace.
With these interfaces defined, in my ASP 4.5 website I can now at the web layer create an HttpContext class based on my IHttpContext and have that class basically wrap the ASP 4.5 HttpContext object. My HttpContext object could then be passed into my library for use.
And in my MVC Core website I can now at the web layer create an HttpContext class based on my IHttpContext and have that class basically wrap the MVC Core HttpContext object. My HttpContext object could then be passed into my library for use just like it was when I was running under the ASP 4.5 environment.
So in the end, my library doesn't know which HttpContext object (4.5 or Core) it is actually accessing under the hood because it just knows that the object is has access to confirms to the IHttpInterface defined in it's library.
One final note, to help navigate name conflicts, I actually named my interfaces this way:
IAppHttpContext
IAppHttpRequest
IAppHttpResponse
IAppHttpSession
IAppHttpRequestHeaders
IAppHttpRequestCookies
Note that not all the functionality that is available in HttpContext 4.5 is available in MVC Core. The two are very similar but there are a few differences. The biggest difference is that MVC Core Session can only store byte arrays or strings whereas 4.5 session can store objects. So my IAppSession only supports storing byte arrays and strings and I have to make sure all my library's session needs work with that (all the objects that I need to store in session need to be serializable).
As you can imagine, implementing this is a bit of work, but in the end you will have a library that can access HttpContext and not care if it's running under a 4.5 HttpContext or a MVC Core HttpContext. Kinda neat.
Good luck.
i too had this same problem. I solved it by adding following dependency in my project.json file to add the http packages to class library
"Microsoft.AspNetCore.Http.Abstractions": "1.1.0"
then i used it like below
public class sampleclass
{
private readonly IHttpContextAccessor context;
public ISession GetSession()
{
return context.HttpContext.Session;
}
}
Thank you. Happy Coding :-)
Reference : http://benjii.me/2016/07/using-sessions-and-httpcontext-in-aspnetcore-and-mvc-core/

How to implement custom membership provider on Asp.net MVC?

I want to implement custom membership provider in my project and I'm using repository pattern in my application.
Here is my project structure: (3 projects)
MyProject (Asp.Net MVC)
MyProject.Model (Entity model classes. I'm using entity framework)
MyProject.Repository (My repository classes)
Where do I implement the custom membership class? Since it's inherit from System.Web.Security; I should create in MyProject?
What is the right way to create custom membership class with Repository pattern?
I would suggest you to create one more project MyProject.Infrastructure that will contain your custom membership classes and other core components related to your application infrastructure.
You have to register the custom membership class through web.config so it's difficult for you inject the dependencies like your database repository to the class and so there is no much gain in implementing a repository pattern in your custom membership provider class. Anyway you have to instantiate a concrete repository implementation inside the membership class.