Aspnetcore: Would we simplify the process of WebApi ?

Created on 2 Apr 2018  ·  24Comments  ·  Source: dotnet/aspnetcore

Sometimes we dont need the Razor、View etc.. It is no use for the simple or WebApi-Interface-Services.
So we can remove some services from the function of UseMvc().
Such as:
https://github.com/NMSAzulX/WebApiMiddleware/blob/master/WebApiMiddleware/WebApiExtentsion.cs

Done Design Spec area-mvc enhancement

Most helpful comment

I hope remove Razor、View etc too, because when we want use corert (https://github.com/dotnet/corert) to compile asp.net core to a single exe, it so big, if remove Razor、View etc, it can make the single exe smaller.

Razor、View is a very bad design today, it is useless, because it mixed server and client script in one file,it is a mess, it is not easy portable to other languge as server(java/node...).

the good design is to use react/angular/vue/javascript... to do all the ui, use json to communicate with the server, so in asp.net core, only the webapi is a must, Razor、View etc are redundant and will make people do bad design.

All 24 comments

Tagging @rynowak .

@NMSAzulX - can you provide more information on why it is a problem to have the extra services from .UseMvc()? In general it's harmless to have extra services, except in some very extreme cases.

Can we at least do $89/hr ?

On Apr 02, 2018, at 08:03 AM, Eilon Lipton notifications@github.com wrote:

Tagging @rynowak .

@NMSAzulX - can you provide more information on why it is a problem to have the extra services from .UseMvc()? In general it's harmless to have extra services, except in some very extreme cases.


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or mute the thread.

@darojuv ?

I hope remove Razor、View etc too, because when we want use corert (https://github.com/dotnet/corert) to compile asp.net core to a single exe, it so big, if remove Razor、View etc, it can make the single exe smaller.

Razor、View is a very bad design today, it is useless, because it mixed server and client script in one file,it is a mess, it is not easy portable to other languge as server(java/node...).

the good design is to use react/angular/vue/javascript... to do all the ui, use json to communicate with the server, so in asp.net core, only the webapi is a must, Razor、View etc are redundant and will make people do bad design.

Hi @tiandian.
What are you trying to achieve? Are you looking for a minimum setup to provide API-only endpoints?
MVC is quite opinionated and it serves the needs of the majority of developers, who are mostly intersted in the combined projects.
If you are not interested in the default setup, you can always start from an empty web application project template, and register only the required services for your application.

Why not just use AddMvcCore and add the stuff you want like the rest of us?

@Eilon Oh, sorry,I described something wrong. I want to add one method (like 'AddWebApi()') separated from the 'AddMvc'. I want a succinct project . And most of my projects are decomposed into front end and back end. Some friends are agree with me. I think we can do a survey to understand the other's thoughts.

I hope separate MVC and WebApi, like mvc5 .

I think the question we need to answer is: what is so bad about the current API UseMvc? I don't think I understand the problem.

@DamianEdwards, do you think we should add an extra extension method to support this ask? Something like AddApi() ?
//cc @rynowak

@glennc, do you have any thoughts regarding this? Thanks!

MVC is a Presentation pattern, and Api is a method that service via an http request .
Implementation an API with MVC pattern is wrong.
MVC and API are two separated concerns .
As you can see you add[ApiController] attribute to separate these concerns.
For an API we need model binding , routing , no need to do it with a MVC Controller.

So it's the naming that bugs you then? The packages are also named Mvc.

@glennc, assigning this to you to come up with proposal about what this should look like.

After little tests, please find some additional information:
I created two differents test cases (ASP.NET Core 3.0 Preview):

"Full MVC":

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc()
        .SetCompatibilityVersion(Microsoft.AspNetCore.Mvc.CompatibilityVersion.Latest);
}

"MVC Light":

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvcCore()
        .AddApiExplorer()
        .AddFormatterMappings()
        .AddDataAnnotations()
        .AddCors()
        .SetCompatibilityVersion(Microsoft.AspNetCore.Mvc.CompatibilityVersion.Latest);
}

The controller is the same:

    [ApiController]
    public class TestController : ControllerBase
    {
        [HttpGet("/test")]
        public string Get() => GC.GetTotalMemory(true).ToString();
    }

And I made the Startup class as lightweight as possible.

Results:

| | IServiceCollection.Count after init | GC.GetTotalMemory(true) |
| - | - | - |
| Full | 227 | ~1.37 MB |
| Light | 126 | ~1.04 MB |

So if I'm not wrong, adding Razor features seems to be not free. I understand that it is possible to do this work manually, but it would be nice to simply have a AddMvcApiOnly method (and maybe a class that would contain a subset of Controller).

It's something we're thinking about for 3.0.0 - for anyone else following what's your proposed set of features that would be included by default?

For instance if we made a method like services.AddControllers() that included:

  • CORS
  • Auth
  • API Explorer (Swagger/OpenAPI)
  • Data Annotations

Is this the right set? Would any of you be surprised for these things to be included, or for something else to be excluded?

Nice!
For information, adding "Authorization" adds 9 services (135 services instead of 126 services).

In the Controller class, the last lines may be as useful as in the "full" MVC (Json(), IActionFilter, IAsyncActionFilter, IDisposable implementations). But in this case a new ApiController class (already with the [ApiController] attribute?) would be necessary. Maybe too much?

For information, adding "Authorization" adds 9 services (135 services instead of 126 services).

I think it would be really surprising if we gave people a new and recommended way of configuring MVC but it didn't include authorization by default. Authorization works because you put attributes on your controllers we recognize those attributes... without AddAuthorization() this would just silently no-op.

Compare this to something like calling View() from a controller without adding the services related to views - this will throw an exception and you will immediately know about the problem.

Data Annotations is similar - it has a very minimal cost if you don't use it, but if it's not included then stuff just no-ops.

I agree! The remark about the number of services for "Authorization" was just an information :-).

I think Razor and MVC should be completely independent of each other. Then Razor could be just a file provider (with additional details about model, view bag etc.).

I hope to do this in 3.0.0, just not 100% sure when yet. @rynowak and I need to get together to design it more and then get it done in a milestone. Hopefully around preview5.

@glennc should this move to preview4?
/cc @rynowak

We've added an AddControllers() extension method in preview 4. Expect to see some more details about this part of the preview 4 announcement.

Was this page helpful?
0 / 5 - 0 ratings