Hi,
Im trying to post a query to my web server but receieve a 500 Internal Server Error without calling the Controller, which never is initialized. I need some help figuring out what I'm doing wrong. I have tried convention based routing and Attribute routing but no luck.
I'm ASP.NET 5 MVC 6, Angular 2 and Typescript. DNX is: 1.0.0-rc1-update1 clr x86
Thanks!
My project.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" } }
My Startup.cs
namespace Pixie { using System; using Microsoft.AspNet.Builder; using Microsoft.AspNet.Hosting; using Microsoft.Extensions.DependencyInjection; using CommandQueryApi.Commands; using CommandQueryApi.Queries; using Microsoft.AspNet.Mvc.Filters; using Microsoft.AspNet.Diagnostics; using Microsoft.AspNet.Mvc.Routing; 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(options => { options.Filters.Add(new FilterMyRoutes()); }); } // 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("queryRouter","query/{*queryName}", 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); } public class FilterMyRoutes : ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext actionContext) { var routes = actionContext.RouteData.DataTokens; base.OnActionExecuting(actionContext); } } }
Here is the call from angular 2 frontend:
public loadData = () => { var query = new LoadDataQuery(); var observable = this.backendCaller.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; };
My Controller:
namespace Pixie.Controllers { using System.IO; using System.Threading.Tasks; using CommandQueryApi.Queries; using Microsoft.AspNet.Authorization; using Microsoft.AspNet.Mvc; using Newtonsoft.Json; using Pallas.Queries; [Route("[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; } [HttpPost("{queryName}")] 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(); } } }