Reputation: 45
I'm trying to create a multiplayer game using Blazor server-side app (RazorComponents) in .Net Core 3.0 preview. I have a SQL lite database and data access layer in my Server project to store the game information. I have a Controller class, also in the Server project, to return objects from the database using the data access layer. My cshtml page in the App project is making an api call (using injected HttpClient) trying to route to my controller, but it does not reach the server.
I have been trying different Http client calls, but none seem to be reaching the controller. I feel like I'm missing something obvious, or configured something incorrectly. I don't have a solid Razor/MVC background, so this is a bit tough for me to troubleshoot. For testing, I'm trying to call a simple method that returns a string
In App.Game.cshtml @functions { } section:
private async Task SaveGame()
{
var result = await Client.GetStringAsync("/api/Game/Test");
}
In Server.Controllers.GameController:
public class GameController : Controller
{
[HttpGet]
[Route("api/Game/Test")]
public string Test()
{
return "test";
}
}
My Server.Startup file registers MVC and Http services like so:
// 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 https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorComponents<App.Startup>();
services.AddMvc();
//Code I got from internet to register httpclient service
if (!services.Any(x => x.ServiceType == typeof(HttpClient)))
{
// Setup HttpClient for server side in a client side compatible fashion
services.AddScoped<HttpClient>(s =>
{
// Creating the URI helper needs to wait until the JS Runtime is initialized, so defer it.
var uriHelper = s.GetRequiredService<IUriHelper>();
return new HttpClient
{
BaseAddress = new Uri(uriHelper.GetBaseUri())
};
});
}
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseStaticFiles();
app.UseRazorComponents<App.Startup>();
app.UseMvc(routes => { routes.MapRoute(name: "default", template: "{controller}/{action}/{id?}"); });
}
When the SaveGame() method is called and client call is executed, I see this in the output:
Microsoft.AspNetCore.Hosting.Internal.GenericWebHostService: Information: Request starting HTTP/1.1 GET http://localhost:59682/api/Game/Test
Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware: Information: Sending file. Request path: '/index.html'. Physical path: '{path to my game}.Server\wwwroot\index.html'
So it seems like it isn't getting properly routed to the controller. I've been googling how other people have done this and it seems I'm doing it the right way. Anyone know what I'm missing?
I've been following tutorials like these: http://learn-blazor.com/architecture/rest-api/ https://medium.freecodecamp.org/how-to-create-an-application-using-blazor-and-entity-framework-core-1c1679d87c7e
Upvotes: 1
Views: 7652
Reputation: 1200
Just in case someone had the same problems, in my case this also worked:
app.UseEndpoints(endpoints =>
{
endpoints.MapDefaultControllerRoute(); // <- this
endpoints.MapBlazorHub();
endpoints.MapFallbackToPage("/index_sse");
});
Upvotes: 3
Reputation: 45586
Try this: put app.UseMvc(routes => { routes.MapRoute(name: "default", template: "{controller}/{action}/{id?}"); });
before calling app.UseStaticFiles(); and app.UseRazorComponents<App.Startup>();
It seems to me that the server is sending index.html instead (StaticFileMiddleware)
Upvotes: 1