3

I have a Blazor WebAssembly Hosted solution (Client and Server) setup with IdentityServer for Authentication. I am looking to do 2 things...

  1. I would like to set up MVC on the Server since I am more comfortable with MVC. The idea for Server Side pages would be for things like Profile Management and accessing Content that I do not want to on the Client.

The Server Startup.cs currently has

public void ConfigureServices(IServiceCollection services)
        {
            ....Condensed

            services.AddControllersWithViews();
            services.AddRazorPages();
        }

public void Configure(IApplicationBuilder app, IWebHostEnvironment env, DataContext dataContext)
        {
           .....Condensed

           app.UseRouting();

            app.UseIdentityServer();
            app.UseAuthentication();
            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapRazorPages();
                endpoints.MapControllers();
                endpoints.MapFallbackToFile("index.html");
            });
        }

  1. With MVC setup on the backend, how can I navigate to these pages from the Client?
1
  • Usually when I want to add a framework to an application, I use VS or the command line to create a new empty application of the type I want. Then I examine the configuration it automatically generated, and piece by piece copy the relevant configuration into my existing application. Commented Apr 29, 2021 at 16:32

1 Answer 1

2

When you create your WebAssembly solution, be sure to check the box "ASP.Net Core Hosted".

This will create three projects: Client, Shared, and Server.

In the server project you will find a Controllers folder. Go ahead and add a controller, such as DummyController.cs

namespace BlazorWASM4.Server.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class DummyController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }
    }
}

Then right-click on your controller method Index and click 'Add View'. Then implement the view (Index.cshtml) like this for example:

<h1>Dummy Page</h1>

Run the project and navigate to localhost:port/Dummy

You should see your new page get displayed.

Sign up to request clarification or add additional context in comments.

3 Comments

I did that and it went into the Blazor routing and said the page could not be found. Thats ultimately why i asked the question here because i thought it should work just as you posted. I was wondering if i needed to change anything within the app.UseEndpoints
I notice though you do have it marked as an APIController. I did not have it marked as an APIController. I will try that and see
@JoeyD, my code for app.UseEndPoints is identical to what you posted in your question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.