Saturday, September 15, 2018

NET Core 2.1: Can you call a controller without the page url updating?

I'm creating a React SPA with .NET Core and I can't find a way to call a controller action, without the page attempting to route to the Action url. Am I thinking about it the wrong way?

​

I essentially want to use a controller as a data access point to call from JavaScript. I don't want it to route anywhere.

​

I have tried:

  • returning the desired object from the controller action
  • returning a JsonResult from the controller action

​

Controller

[Route("api/[controller]")] public class SampleDataController : Controller 

​

Action

[HttpGet("[action]")] public List<UpdateLog> UpdateSandbox() { return new List<UpdateLog>() } 

Startup.cs

 public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Error"); app.UseHsts(); } app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseSpaStaticFiles(); app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller}/{action=Index}/{id?}"); routes.MapRoute( routes.MapSpaFallbackRoute( name: "spa-fallback", defaults: new { controller = "SampleData", action = "Home" }); }); app.UseSpa(spa => { spa.Options.SourcePath = "ClientApp"; if (env.IsDevelopment()) { spa.UseReactDevelopmentServer(npmScript: "start"); } }); } 

​

Any help hugely appreciated!

NET Core 2.1: Can you call a controller without the page url updating? Click here
  • Blogger Comment
  • Facebook Comment

0 comments:

Post a Comment

The webdev Team