I am new to react but not to asp.net core application development. I am trying to create a react application with asp.net core + react template in visual studio. I am trying to do a Asp.Net MVC route first to call controller action which has [Authorize] attribute to make sure user is authenticated. I do not have any thing to show in Asp.Net MVC view. I want to immediately redirect user to react default route once user is authenticated via asp.net mvc controller action. Is there any specific routing mechanism to achieve that.
Right now my application goes through controller and action based on route and stops at view defined by controller action. I am trying to understand how to redirect user to use react route now. I tried using return redirecttoaction and returnredirecttoroute but no luck.
My Asp.Net Core MVC Action
[Authorize]
public ActionResult Index()
{
var IsAuthenticated = HttpContext.User.Identity.IsAuthenticated;
var UserName = "Guest";
if (IsAuthenticated)
{
UserName = HttpContext.User.Identity.Name;
}
TempData["userName"] = UserName;
//return View();
return Redirect("my react first page");
}
I tried return Redirect("my react first page");
My Startup file method used for routing
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.UseAuthentication();
//MVC Part. I am trying this to authorize as FIRST step
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=DataAccess}/{action=Index}/{id?}");
});
// React part I am trying this to be called once Authentication is done in controller action. Trying to call this as a SECOND step
app.UseSpa(spa =>
{
spa.Options.SourcePath = "ClientApp";
if (env.IsDevelopment())
{
spa.UseReactDevelopmentServer(npmScript: "start");
}
});
}
If I do a redirect and force the react route will there be any issue missing react route features? I see this spa.UseReactDevelopmentServer(npmScript: "start"); is taking more time showing a timeout if I do a redirect. Any solution where User can be redirected to controller action do all authorization and use react default routing mechanism?
Is there any option to run react server first and do routing as starting server is taking more time leading to timeouts.
Note: I used Create-React-App to create my app.