CustomModelBinder on properties of a Dto class - asp.net-mvc

I have a DtoClass which has properties of a specific class, I don't want to have a CustomModelBinder for the DtoClass but for the class of its properties; I am using asp.net core 3.1.
My ModelBinder Class is:
public class SessionIdModelBinder : IModelBinder
{
public Task BindModelAsync(ModelBindingContext bindingContext)
{
Guard.Against.Null(bindingContext, nameof(bindingContext));
var modelName = bindingContext.ModelName;
var valueProviderResult = bindingContext.ValueProvider.GetValue(modelName);
if (valueProviderResult == ValueProviderResult.None)
return Task.CompletedTask;
var sessionId = SessionId.Parse(valueProviderResult.FirstValue);
if (sessionId.IsFailure)
{
bindingContext.ModelState.AddModelError(modelName, sessionId.Errors.First().Message);
bindingContext.Result = ModelBindingResult.Failed();
return Task.CompletedTask;
}
bindingContext.Result = ModelBindingResult.Success(sessionId.Data);
return Task.CompletedTask;
}
}
The Dto class is like:
public class MergeSessionsDto
{
[ModelBinder(BinderType = typeof(SessionIdModelBinder), Name = nameof(OldSession))]
public SessionId OldSession { get; set; }
[ModelBinder(BinderType = typeof(SessionIdModelBinder), Name = nameof(NewSession))]
// [BindProperty(BinderType = typeof(SessionIdModelBinder), Name = nameof(NewSession))]
public SessionId NewSession { get; set; }
}
The action in my controller is:
public async Task<IActionResult> MergeSessions([FromBody] MergeSessionsDto dto)
{
var result = DoTheMerge(dto.OldSession, dto.NewSession);
return result;
}
in the startup class I also registered the ModelBinderProvider :
services.AddControllers(options=> options.ModelBinderProviders.Insert(0, new MyCustomModelBinderProvider()))
which is like:
public sealed class MyCustomModelBinderProvider : IModelBinderProvider
{
public IModelBinder GetBinder(ModelBinderProviderContext context)
{
Guard.Against.Null(context, nameof(context));
if (context.Metadata.ModelType == typeof(SessionId))
return new BinderTypeModelBinder(typeof(SessionIdModelBinder));
return null;
}
}
No matter which approach I am using, either [ModelBinder], [BindProperty] attributes, or global registration, SessionModelBinder is not called, and I am getting this error:
Exception: Invalid error serialization: 'The dto field is required.'

Related

I did dependency injection but the value is returning null in asp.net mvc

public class HomeController : Controller
{
private readonly IUserDAService _user = DependencyResolver.Current.GetService<IUserDAService>();
public ActionResult About()
{
_user.GetAdminUser(1);
return View();
}
}
_user returns null. So _user.GetAdminUser(1) also gives System.NullReferenceException error.
public interface IUserDAService
{
int GetAdminUser(int id);
}
public class UserDAService : IUserDAService
{
public int GetAdminUser(int id)
{
using (var connection= DBConnection.CreateConnection())
{
connection.Open();
return connection.CacheQuery<int>(StoredProcedure.GetAdminUser, new { Id = id }, commandType: CommandType.StoredProcedure).FirstOrDefault();
}
}
}

Error When Seeding Users - MVC5 EF6 Identity 2.0

We're working on our seed method for our database, and have tried a variety of different things to make it work, but it keeps erroring out.
Our seed method is
protected override void Seed(WebApplication1.Models.ApplicationDbContext context)
{
if (!context.Roles.Any(r => r.Name == "Admin"))
{
var store = new RoleStore<IdentityRole>(context);
var manager = new RoleManager<IdentityRole>(store);
var role = new IdentityRole {Name = "Admin"};
manager.Create(role);
}
if (!context.Users.Any(u => u.UserName == "Admin"))
{
var store = new UserStore<ApplicationUser>(context);
var manager = new UserManager<ApplicationUser>(store);
var user = new ApplicationUser {UserName = "Admin"};
manager.Create(user, "ChangeItAsap!");
manager.AddToRole(user.Id, "Admin");
}
}
The error is
The type 'WebApplication1.Models.ApplicationUser' cannot be used as type parameter 'TUser' in the generic type or method 'Microsoft.AspNet.Identity.EntityFramework.UserStore<TUser>'. There is no implicit reference conversion from 'WebApplication1.Models.ApplicationUser' to 'Microsoft.AspNet.Identity.EntityFramework.IdentityUser'.
With the following line being the cause
var store = new UserStore<ApplicationUser>(context);
I'm not entirely sure what could be causing this, as I've never ran into this issue in the past. any help would be appreciated.
EDIT
Here is the definition
public class ApplicationUserLogin : IdentityUserLogin<string> { }
public class ApplicationUserClaim : IdentityUserClaim<string> { }
public class ApplicationUserRole : IdentityUserRole<string> { }
// Must be expressed in terms of our custom Role and other types:
public class ApplicationUser
: IdentityUser<string, ApplicationUserLogin,
ApplicationUserRole, ApplicationUserClaim>
{
public ApplicationUser()
{
this.Id = Guid.NewGuid().ToString();
// Add any custom User properties/code here
}
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(ApplicationUserManager manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
}
public class ApplicationRole : IdentityRole<string, ApplicationUserRole>
{
public ApplicationRole()
{
this.Id = Guid.NewGuid().ToString();
}
public ApplicationRole(string name)
: this()
{
this.Name = name;
}
// Add any custom Role properties/code here
}
public class ApplicationDbContext
: IdentityDbContext<ApplicationUser, ApplicationRole,
string, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{
public ApplicationDbContext()
: base("DefaultConnection")
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
// Most likely won't need to customize these either, but they were needed because we implemented
// custom versions of all the other types:
public class ApplicationUserStore
: UserStore<ApplicationUser, ApplicationRole, string,
ApplicationUserLogin, ApplicationUserRole,
ApplicationUserClaim>, IUserStore<ApplicationUser, string>,
IDisposable
{
public ApplicationUserStore()
: this(new IdentityDbContext())
{
base.DisposeContext = true;
}
public ApplicationUserStore(DbContext context)
: base(context)
{
}
}
public class ApplicationRoleStore
: RoleStore<ApplicationRole, string, ApplicationUserRole>,
IQueryableRoleStore<ApplicationRole, string>,
IRoleStore<ApplicationRole, string>, IDisposable
{
public ApplicationRoleStore()
: base(new IdentityDbContext())
{
base.DisposeContext = true;
}
public ApplicationRoleStore(DbContext context)
: base(context)
{
}
}

Get custom attribute for parameter when model binding

I've seen a lot of similar posts on this, but haven't found the answer specific to controller parameters.
I've written a custom attribute called AliasAttribute that allows me to define aliases for parameters during model binding. So for example if I have: public JsonResult EmailCheck(string email) on the server and I want the email parameter to be bound to fields named PrimaryEmail or SomeCrazyEmail I can "map" this using the aliasattribute like this: public JsonResult EmailCheck([Alias(Suffix = "Email")]string email).
The problem: In my custom model binder I can't get a hold of the AliasAttribute class applied to the email parameter. It always returns null.
I've seen what the DefaultModelBinder class is doing to get the BindAttribute in reflector and its the same but doesn't work for me.
Question: How do I get this attribute during binding?
AliasModelBinder:
public class AliasModelBinder : DefaultModelBinder
{
public static ICustomTypeDescriptor GetTypeDescriptor(Type type)
{
return new AssociatedMetadataTypeTypeDescriptionProvider(type).GetTypeDescriptor(type);
}
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var value = base.BindModel(controllerContext, bindingContext);
var descriptor = GetTypeDescriptor(bindingContext.ModelType);
/*************************/
// this next statement returns null!
/*************************/
AliasAttribute attr = (AliasAttribute)descriptor.GetAttributes()[typeof(AliasAttribute)];
if (attr == null)
return null;
HttpRequestBase request = controllerContext.HttpContext.Request;
foreach (var key in request.Form.AllKeys)
{
if (string.IsNullOrEmpty(attr.Prefix) == false)
{
if (key.StartsWith(attr.Prefix, StringComparison.InvariantCultureIgnoreCase))
{
if (string.IsNullOrEmpty(attr.Suffix) == false)
{
if (key.EndsWith(attr.Suffix, StringComparison.InvariantCultureIgnoreCase))
{
return request.Form.Get(key);
}
}
return request.Form.Get(key);
}
}
else if (string.IsNullOrEmpty(attr.Suffix) == false)
{
if (key.EndsWith(attr.Suffix, StringComparison.InvariantCultureIgnoreCase))
{
return request.Form.Get(key);
}
}
if (attr.HasIncludes)
{
foreach (var include in attr.InlcludeSplit)
{
if (key.Equals(include, StringComparison.InvariantCultureIgnoreCase))
{
return request.Form.Get(include);
}
}
}
}
return null;
}
}
AliasAttribute:
[AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
public class AliasAttribute : Attribute
{
private string _include;
private string[] _inlcludeSplit = new string[0];
public string Prefix { get; set; }
public string Suffix { get; set; }
public string Include
{
get
{
return _include;
}
set
{
_include = value;
_inlcludeSplit = SplitString(_include);
}
}
public string[] InlcludeSplit
{
get
{
return _inlcludeSplit;
}
}
public bool HasIncludes { get { return InlcludeSplit.Length > 0; } }
internal static string[] SplitString(string original)
{
if (string.IsNullOrEmpty(original))
{
return new string[0];
}
return (from piece in original.Split(new char[] { ',' })
let trimmed = piece.Trim()
where !string.IsNullOrEmpty(trimmed)
select trimmed).ToArray<string>();
}
}
Usage:
public JsonResult EmailCheck([ModelBinder(typeof(AliasModelBinder)), Alias(Suffix = "Email")]string email)
{
// email will be assigned to any field suffixed with "Email". e.g. PrimaryEmail, SecondaryEmail and so on
}
Gave up on this and then stumbled across the Action Parameter Alias code base that will probably allow me to do this. It's not as flexible as what I started out to write but probably can be modified to allow wild cards.
what I did was make my attribute subclass System.Web.Mvc.CustomModelBinderAttribute which then allows you to return a version of your custom model binder modified with the aliases.
example:
public class AliasAttribute : System.Web.Mvc.CustomModelBinderAttribute
{
public AliasAttribute()
{
}
public AliasAttribute( string alias )
{
Alias = alias;
}
public string Alias { get; set; }
public override IModelBinder GetBinder()
{
var binder = new AliasModelBinder();
if ( !string.IsNullOrEmpty( Alias ) )
binder.Alias = Alias;
return binder;
}
}
which then allows this usage:
public ActionResult Edit( [Alias( "somethingElse" )] string email )
{
// ...
}

asp .net mvc custom model binding fails

I wrote a custom binder, but i can't seem to get it running.
Here's my code:
public abstract class SlideItem
{
public string PlaceHolderName { get; set; }
public uint PlaceHolderID { get; set; }
public uint ItemType { get; set; }
}
public class TitleSlideItem : SlideItem
{
...
public TitleSlideItem()
{
ItemType = 1;
}
}
public class ParagraphSlideItem : SlideItem
{
...
public ParagraphSlideItem()
{
ItemType = 2;
}
}
public class SlideItemBinder : IModelBinder
{
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
System.Diagnostics.Trace.WriteLine("BindModel is working");
var values = (ValueProviderCollection)bindingContext.ValueProvider;
var placeHolderName = (string)values.GetValue("PlaceHolderName").ConvertTo(typeof(string));
var placeHolderID = (uint)values.GetValue("PlaceHolderID").ConvertTo(typeof(uint));
var itemType = (uint)values.GetValue("ItemType").ConvertTo(typeof(uint));
switch (itemType)
{
case 1:
System.Diagnostics.Trace.WriteLine("TitleSlideItemBinder");
return (SlideItem)new TitleSlideItem { PlaceHolderName = placeHolderName };
case 2:
System.Diagnostics.Trace.WriteLine("ParagraphSlideItem");
return (SlideItem)new ParagraphSlideItem { PlaceHolderName = placeHolderName };
case 3:
System.Diagnostics.Trace.WriteLine("TextSlideItem");
return (SlideItem)new TextSlideItem { PlaceHolderName = placeHolderName };
case 4:
System.Diagnostics.Trace.WriteLine("PictureSlideItem");
return (SlideItem)new PictureSlideItem { PlaceHolderName = placeHolderName };
default:
System.Diagnostics.Trace.WriteLine("this should never-ever-ever-ever happen");
//this should never-ever-ever-ever happen
return (SlideItem)new TextSlideItem { PlaceHolderName = placeHolderName };
}
}
}
I added this to my global.asax.cs:
ModelBinders.Binders.Add(new KeyValuePair<Type, IModelBinder>(typeof(SlideItem), new SlideItemBinder()));
but when i try to run this:
[System.Web.Http.HttpPost]
public HttpResponseMessage Create(IList<SlideContent> l)
{
...
}
i still get a System.MissingMethodException saying: cannot create abstract class
Where am i going wrong?
Sincerely,
Zoli
Check this link.you might find the solution here: ASP.NET MVC 2 - Binding To Abstract Model
The answer has a custom model binder.

What's the best way to bind ExtJs 4 Grid filter info to asp.net mvc action parameters?

What's the best way to bind ExtJs 4 Grid filter info to asp.net mvc action parameters?
I wrote these helper classes:
public class ExtFilterInfo
{
public string Field { get; set; }
public ExtFilterData Data { get; set; }
}
public class ExtFilterData
{
public string Type { get; set; }
public string Value { get; set; }
}
Here is the Action:
public ActionResult Grid(int start, int limit, string sort, ExtFilterInfo[] filter)
The QueryString looks something like this:
_dc:1306799668564
filter%5B0%5D%5Bfield%5D:Nome
filter%5B0%5D%5Bdata%5D%5Btype%5D:string
filter%5B0%5D%5Bdata%5D%5Bvalue%5D:nu
page:1
start:0
limit:20
A custom model binder looks like it could fit the bill:
public class ExtFilterInfoModelBinder : DefaultModelBinder
{
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var filter = (ExtFilterInfo)base.BindModel(controllerContext, bindingContext);
var field = bindingContext.ValueProvider.GetValue(bindingContext.ModelName + "[field]");
if (field != null)
{
filter.Field = field.AttemptedValue;
}
var type = bindingContext.ValueProvider.GetValue(bindingContext.ModelName + "[data][type]");
if (type != null)
{
if (filter.Data == null)
{
filter.Data = new ExtFilterData();
}
filter.Data.Type = type.AttemptedValue;
}
var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName + "[data][value]");
if (value != null)
{
if (filter.Data == null)
{
filter.Data = new ExtFilterData();
}
filter.Data.Value = value.AttemptedValue;
}
return filter;
}
}
which could be registered in Application_Start:
ModelBinders.Binders.Add(typeof(ExtFilterInfo), new ExtFilterInfoModelBinder());
and now the filter collection which your controller action takes as argument should be bound correctly.

Resources