C# 来自Angular的呼叫控制器出现问题
从angular ClientApp服务运行控制器时出现问题 无法理解控制器为什么没有接到任何呼叫 我的角度服务代码:C# 来自Angular的呼叫控制器出现问题,c#,angular,asp.net-core-mvc,C#,Angular,Asp.net Core Mvc,从angular ClientApp服务运行控制器时出现问题 无法理解控制器为什么没有接到任何呼叫 我的角度服务代码: export class MediaService extends BaseService { constructor(private http: Http, configService: ConfigService) { super(configService); } getMedia(mediaId: string) {
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>();
public void配置服务(IServiceCollection服务)
{
DbInitializer.AdminV2Initialize(Configuration.GetConnectionString(“DefaultConnection”));
//从应用程序设置获取选项
var jwtappsetingoptions=Configuration.GetSection(nameof(jwtissueoptions));
Configure(Configuration.GetSection(nameof(SmtpClientConfig));
services.TryAddTransient();
services.addScope();
services.addScope();
services.addScope();
services.addScope();
services.addScope();
services.addScope();
services.addScope();
services.addScope();
services.AddSingleton();
我也有AuthController,它工作得很好。
当我使用MediaController时,只有500个内部服务器错误(
控制器甚至不尝试初始化-有什么问题吗?您希望在这里调用哪种方法?MediaController的索引方法您使用的是.net core mvc?还是.net core webapi?我相信它是mvc。选择webapi,mvc,我们通常不使用Angular调用哪种方法?MediaController的索引方法你使用的是.net core mvc还是.net core webapi?我相信它是mvc。选择webapi,mvc我们通常不会与angular一起使用