0

I have one .NET Core MVC application that provides a dashboard to represent statistics and so on.

I would like to use this MVC application as a Middleware so that I can use it the same way than Hangfire, NSwag and so on.

So in my main project I would like to do the following:

  1. Import my NuGet package (the Dashboard MVC Application)
  2. app.UseDashboard(options...)

I already have a Middleware class inside my Dashboard MVC App with the following:

 public static class JwtDashboardMiddleware
 {
    public static void UseMyDashboard(this IApplicationBuilder app)
    {
        // 1. Provide an endpoint(/ myDashboard)
        app.Map("/myDashboard", app =>
            app.Run(async (context) =>
            {
                string message = "Hello Middleware";
                //2. This page contains a simple form, Submit the form to redirect to another for
                await context.Response.BodyWriter.WriteAsync(Encoding.UTF8.GetBytes(message));
            }));
    }
 }

I also created a NuGet Package with the Dashboard MVC App and it is working, but in the Main Application and also in the in the Dashboard MVC App it cannot access the Views and so on.

So from the Middleware inside the Dasboard MVC app how do I access the controllers and how do I integrate this with other MVC applications to achieve the points mentioned above?

Btw. I also tried to redirect to the controller view, but the view is just not accessible ? Do I need to compile the views to be included in the NuGet package or how does this work?

The main question is: How do I use my controllers and views inside the Middleware?

1 Answer 1

1

You cannot embed whole MVC application into one static route.

NSwag for example, on the /swagger endpoint, serves a static html file, which is basing on swagger.json file generated from the API itself

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

4 Comments

Ok and how can you put static files in the Middleware ? So basically, swagger.json serves as input for the static file ?
And do you have a link to the Git Repo of their Middleware, I can't find it ?
yes, that how it works: Here is the UseSwagger extension: github.com/domaindrivendev/Swashbuckle.AspNetCore/blob/master/…
and here: in RespondWithSwaggerJson it's writing to the response; github.com/domaindrivendev/Swashbuckle.AspNetCore/blob/…

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.