gpt4 book ai didi

c# - Asp.net 5 MVC 6 500 内部服务器错误 - Angular 2

转载 作者:太空宇宙 更新时间:2023-11-03 12:49:48 25 4
gpt4 key购买 nike

我试图向我的 Web 服务器发布查询,但在没有调用从未初始化的 Controller 的情况下收到 500 Internal Server Error。我需要一些帮助来弄清楚我做错了什么。我尝试过基于约定的路由和属性路由,但没有成功。

我是 ASP.NET 5 MVC 6, Angular 2, typescript ,DNX 是:1.0.0-rc1-update1 clr x86,Visual Studio 2015 更新 1,IIS Express 10

谢谢!

从 Angular2 调用:

public class DataLoader
{
public loadData = () => {
var query = new LoadDataQuery();
var observable = this.get(query);
observable.subscribe((data: any) => {
console.log("Data from server: ", data);
});
};


public get = <TResult>(query: Query<TResult>): Observable<TResult> => {
var queryName = this.getName(query);
var url = `/query/${queryName}`;
return this.httpPost<TResult>(url, query);
};


private getName = (action: any): string => {
var regex = /function (.{1,})\(/;
var results = (regex).exec(action.constructor.toString());
return (results && results.length > 1) ? results[1] : "";
};

private httpPost = <TResult>(url: string, data: any): Observable<TResult> => {
var observable = this.http.post(url, data)
.map<TResult>(response => response.json());
return observable;
};
}

我的项目.json:

{
"version": "1.0.0",
"compilationOptions": {
"emitEntryPoint": true
},
"dependencies": {
"Microsoft.AspNet.IISPlatformHandler": "1.0.0-rc1-final",
"Microsoft.AspNet.Server.Kestrel": "1.0.0-rc1-final",
"Microsoft.AspNet.StaticFiles": "1.0.0-rc1-final",
"Microsoft.AspNet.Diagnostics": "1.0.0-rc1-final",
"Microsoft.AspNet.Mvc": "6.0.0-rc1-final",
"Movestic.Styles": "1.0.0-*"
},
"frameworks": {
"dnx46": {
"dependencies": {
"Pixie.CommandQueryApi": "1.0.0-*"
}
}
},
"exclude": [
"wwwroot",
"node_modules"
],
"publishExclude": [
"**.user",
"**.vspscc"
],
"commands": {
"web": "Microsoft.AspNet.Server.Kestrel"
}
}

我的 Startup.cs

我也尝试过属性路由...

public class Startup
{
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<IQueryExecutor, QueryExecutor>();
services.AddSingleton<IQueryTypeCollection, QueryTypeReflectionCollection>();
services.AddSingleton<ICommandExecutor, CommandExecutor>();
services.AddSingleton<ICommandTypeCollection, CommandTypeReflectionCollection>();

services.AddMvc();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app)
{
app.UseMvc(routes =>
{
routes.MapRoute(
name: "queryRouter",
template: "query/{*queryName}",
defaults: new { controller = "Query", action = "Handle" });
});

app.UseDeveloperExceptionPage();

// Add the platform handler to the request pipeline.
app.UseIISPlatformHandler();
app.UseDefaultFiles();
app.UseStaticFiles();
}

// Entry point for the application.
public static void Main(string[] args) => WebApplication.Run<Startup>(args);
}

我的 Controller :

public class QueryController : Controller
{
private readonly IQueryExecutor queryExecutor;
private readonly IQueryTypeCollection queryTypeCollection;

public QueryController(IQueryExecutor queryExecutor, IQueryTypeCollection queryTypeCollection)
{
this.queryExecutor = queryExecutor;
this.queryTypeCollection = queryTypeCollection;
}

public async Task<IActionResult> Handle(string queryName)
{
var queryType = this.queryTypeCollection.GetTypeForQueryName(queryName);
var reader = new StreamReader(this.Request.Body);
var json = await reader.ReadToEndAsync();
var query = JsonConvert.DeserializeObject(json, queryType) as IQuery;

await this.queryExecutor.ExecuteAsync(query);
return this.Ok();
}
}

最佳答案

通过在 Startup.cs Configure(IApplicationBuilder app) 中打开 app.UseDeveloperExceptionPage(); 解决了问题

关于c# - Asp.net 5 MVC 6 500 内部服务器错误 - Angular 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35990898/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com