私が取り組んでいるプロジェクトにはいくつかのDTOがあります。 AutoMapperを使ってマッピングを作成しています。いずれかのマッピングを除くすべてのマッピングが機能します。 LINQ Method Syntaxを使用してデータを取得するとき、Null参照を取得しているため、わかります。私のDTO Nullはなぜですか?
MappingProfile.cs
using AutoMapper;
using GigHub.Controllers.Api;
using GigHub.Dtos;
using GigHub.Models;
namespace GigHub.App_Start
{
public class MappingProfile : Profile
{
public MappingProfile()
{
Mapper.CreateMap<ApplicationUser, UserDto>();
Mapper.CreateMap<Gig, GigDto>();
Mapper.CreateMap<Notification, NotificationDto>();
Mapper.CreateMap<Following, FollowingDto>();
}
}
}
Global.asax.cs
using AutoMapper;
using GigHub.App_Start;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
namespace GigHub
{
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
Mapper.Initialize(c => c.AddProfile<MappingProfile>());
GlobalConfiguration.Configure(WebApiConfig.Register);
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("elmah.axd");
}
}
}
Following.cs
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace GigHub.Models
{
// Alternatively, this class could be called Relationship.
public class Following
{
[Key]
[Column(Order = 1)]
public string FollowerId { get; set; }
[Key]
[Column(Order = 2)]
public string FolloweeId { get; set; }
public ApplicationUser Follower { get; set; }
public ApplicationUser Followee { get; set; }
}
}
FollowingDto.cs:とにかく、ここで私が関連していると信じて、すべてのコードがあります
namespace GigHub.Dtos
{
public class FollowingDto
{
public string FolloweeId { get; set; }
}
}
FollowingsController.cs
using System.Linq;
using System.Web.Http;
using GigHub.Dtos;
using GigHub.Models;
using Microsoft.AspNet.Identity;
namespace GigHub.Controllers.Api
{
[Authorize]
public class FollowingsController : ApiController
{
private ApplicationDbContext _context;
public FollowingsController()
{
_context = new ApplicationDbContext();
}
//CheckFollow is what I am using to test the Dto
[HttpGet]
public bool CheckFollow(FollowingDto dto)
{
var userId = User.Identity.GetUserId();
if (_context.Followings.Any(f => f.FolloweeId == userId && f.FolloweeId == dto.FolloweeId))
return true;
else
return false;
}
[HttpPost]
public IHttpActionResult Follow(FollowingDto dto)
{
var userId = User.Identity.GetUserId();
if (_context.Followings.Any(f => f.FolloweeId == userId && f.FolloweeId == dto.FolloweeId))
return BadRequest("Following already exists.");
var following = new Following
{
FollowerId = userId,
FolloweeId = dto.FolloweeId
};
_context.Followings.Add(following);
_context.SaveChanges();
return Ok();
}
}
}
WebApiConfig.cs
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System.Web.Http;
namespace GigHub
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
var settings = GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings;
settings.ContractResolver = new CamelCasePropertyNamesContractResolver();
settings.Formatting = Formatting.Indented;
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
}
私はツールの問題を疑うが、私は私のAPIをテストするために郵便配達を使用してきました。このDtoの中で唯一異なる点は、Automapperがマッピングを作成する列に、モデルのキーおよび列の順序属性が含まれていることです。なぜそれが重要かわかりません。私はすでにこの問題についてオンラインで検索しましたが、私はすでにそれらをやっているので解決策は役に立たなかった。私はなぜNull参照を取得することができます投稿したコードから誰かが言うことができますか?技術的には、PostmanのエラーはSystem.Exception.TargetExceptionと言っていますが、私が理解しているところでは、LINQクエリが結果を返さないことを意味します。
私のデータベース内にあったFolloweeIdという文字列を渡し、APIのCheckFollowアクションが機能しているため、私のクエリが機能することが分かります。だから私はDtoが正しくマップされていないと仮定することができます。