Welcome to JiKe DevOps Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
216 views
in Technique[技术] by (71.8m points)

asp.net - How to do single page routing in dotnet 3

I have an application which is built using both client side routing using react and server side routing using MapRazorPages. I need to map tasks/* to a specific view and map everything to razor pages. I'm completely new to dotnet so I don't really know what I'm doing but so far as I can tell I should be able to do something like this:

~/Startup.cs:

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapRazorPages();
            endpoints.MapControllerRoute("tasks", "{controller=Tasks}/{action=Index}/{id?}");
        });

~/Controllers/TasksController.cs:

using Microsoft.AspNetCore.Mvc;

namespace Web.API.Controllers.UI
{
  public class TasksController : Controller
  {
    public IActionResult Index()
    {
        return View("../../Pages/Tasks");
    }
  }
}

But that returns an empty response and I'm guessing also wouldn't work on anything other than tasks/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

Please log in or register to answer this question.

1 Answer

0 votes
by (71.8m points)

Here is a demo:

TasksController:

public class TasksController : Controller
    {
        public IActionResult Index()
        {
            return RedirectToPage("/Tasks/Tasks");
        }
    }

Pages/Tasks/Tasks.cshtml:

@page
@model RazorPageDemo.Pages.Tasks.TasksModel
@{
}
<h1>Tasks-Razor Page!</h1>

result: enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to JiKe DevOps Community for programmer and developer-Open, Learning and Share
...