我在从Angle ClientApp服务运行Controller时遇到问题。
不明白为什么Controller没有接到任何电话。
我的角度服务代码:
export class MediaService extends BaseService {
constructor(private http: Http, configService: ConfigService) {
super(configService);
}
getMedia(mediaId: string) {
return this.http
.get(
'api/media',
{
params: {
id: mediaId
},
headers: this.getBaseHttpHeaders()
}).map(result => {
return result.json();
})
.map(result => {
if (result.status === 1) {
return { success: true, data: result.data };
}
else {
return { success: false, data: result.data };
}
});
}
MediaController.cs
[Authorize(Policy = "UserExists")]
[Produces("application/json")]
[Route("api/media")]
public class MediaController : BaseController
{
private readonly IMediaService _mediaService;
private readonly ICollectionPhotosService _collectionPhotosService;
public MediaController(IUserService userService, IMediaService mediaService, ICollectionPhotosService collectionPhotosService) : base(userService)
{
_mediaService = mediaService;
_collectionPhotosService = collectionPhotosService;
}
[HttpGet]
public IActionResult Index(string id, string collectionPhotoId = null)
{
try
{
Guid guidId = Guid.Empty;
Guid collectionPhotoGuidId = Guid.Empty;
if ((!Guid.TryParse(id, out guidId) && collectionPhotoId == null) || (!Guid.TryParse(collectionPhotoId, out collectionPhotoGuidId) && id == null))
{
return NotFound();
}
if (string.IsNullOrWhiteSpace(UserId))
{
return GenerateBadResult("Incorrect user");
}
var isItemMedia = guidId != Guid.Empty;
return isItemMedia ? HandleMediaItem(guidId, UserId) : HandleCollectionPhotoItem(collectionPhotoGuidId, UserId);
}
catch (Exception)
{
return GenerateBadResult("Some error occured");
}
}
BaseService.cs
public class BaseController : Controller
{
protected readonly IUserService _userService;
public BaseController(IUserService userService)
{
_userService = userService;
}
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
DbInitializer.AdminV2Initialize(Configuration.GetConnectionString("DefaultConnection"));
// Get options from app settings
var jwtAppSettingOptions = Configuration.GetSection(nameof(JwtIssuerOptions));
services.Configure<SmtpClientConfig>(Configuration.GetSection(nameof(SmtpClientConfig)));
services.TryAddTransient<IHttpContextAccessor, HttpContextAccessor>();
services.AddScoped<ITokenService, TokenService>();
services.AddScoped<IEmailProvider, EmailProvider>();
services.AddScoped<IUserService, UserService>();
services.AddScoped<IMediaService, MediaService>();
services.AddScoped<ICollectionService, CollectionService>();
services.AddScoped<IEmailService, EmailService>();
services.AddScoped<ILocationService, LocationService>();
services.AddScoped<ICollectionPhotosService, CollectionPhotosService>();
services.AddSingleton<IUserDataCSVGenerator, UserDataCSVGenerator>();
我也有AuthController,它工作正常。 当我使用MediaController时,我只有500个内部服务器错误( 控制器甚至不尝试初始化-什么问题?