BLAZOR + MVC - passing data from Blazor page to MVC controller - asp.net

I have MVC project, which uses some Blazor pages. What I need now is to pass data back from Blazor page to MVC controller. Is that even possible? My solution is to write data to the DB and get them after in MVC...but I don't like this way. Someone knows a better solution?
Thanks.

There is no built-in functionality for this, you need to use an HTTP client, such as HttpClient. See an example on the Microsoft documentation site here: https://learn.microsoft.com/en-us/aspnet/core/blazor/call-web-api?view=aspnetcore-3.1.

Related

ASP.net web application structure confusing

I've explored the ASP.net web API framework and MVC 4. I understand the point of using web API that only return data then in client side we use javascript, ajax to handle all actions via HTTP verbs. On the other hand, MVC controller can return data and view we can handle event via [HttpPost] in controller.
I saw some people they make 2 projects in a solution: one is Web API in MVC 4 and the other is MVC 4 Internet Application. Is it a good idea? I didnt see the relation between two kind of projects. Can anyone explain me the how two project can communicate with each other, and what is the advantage and disadvantage of that way?
Note: in this solution he/she still uses Httppost to handle event and use MVC controller return view with Model binding.
Well I guess this is going to be a pragmatic conversation...
For starters, it seems like the Asp.net WebApi and Asp.net MVC are going to be aligned in Asp.Net V-Next, so there is going to be One base controller class that will return what ever datatype you wish (ActionResult, Json etc).
And from the recent years experience I think the trend is to have a WebApi that exposes data and a portal(client) that is just a JS web application with no C# code that just calls the Api from the browser and does what it needs to do.
So that way we basically save one hup to the server(the MVC server) and we can directly call the API from the browser.
However some people would still argue about load balancing, scaling, caching etc that you could have if you keep the MVC tier there but still all those things could be done in the WebApi and JS application too...
So long story short, I think if you want to move with the trend I think you should go with a RESTful API and have your client to call it from the browser directly with JavaScript...

Do I need ASP.NET MVC 5 SPA template?

I want to build responsive SPA web application. I followed old paradigms before and the way it is supposed to be done nowadays is not very familiar to me, so sorry if my question seems unadequate.
Now I'm trying to understand the motivation behind this MVC 5 SPA template: why would I need to use MVC and Web API in one application?
My app will not act as a service. I can simply return JSON from my action methods. If in my case it is absolutely not neccessary, what factors could change the situation in favor of using both WebAPI and MVC at the same time?
You don't have to but it is useful.
I'm usually using MVC for
generating initial html file for my SPA through index.cshtml
(bundles + some customization of initial html code)
generating dynamic JavaScript code (usually for something like configuration angular modules)
Web API or OData kicks in aferwards providing endpoints for your favourite SPA framework (angular / knockout etc.) returning json or even xml if you need.

Ext JS with C# Backend

Im currently starting a new WebProject. The plan is to generate the entire frontend via extjs and communicate with a C# .Net Backend (via ExtDirect !? )...
My problem is that i dont really know what VisualStudio Project Type i should use..
The backend should only handle the requests from the Ext JS (and communicate with an other EntityFramework Projekt though thats not a problem)... Are there any tutorials around? I didnt find something like that. I am not sure if i have to use an ASP Web Form Project because i didnt planned to use any ASP controls or something like that. Like i said i want the frontend to be generated by EXT JS only.
Please help. Im glad about any input :)
I've built a project with ASP.Net MVC and extjs as the front end. You could also use something like a WCF service to handle your server side calls but the ASP.Net MVC framework works fine as well.
I used an Ajax proxy in my stores that call my ASP.Net controllers which then call the database and I return the result back as json.
I used this project TraXPLORER = ExtJS 4 MVC + ASP.NET MVC 3 + CRUD + REST to help me get started.
You also may want to check out Ext.Net as it's a wrapper for the extjs library for .Net apps.

Asp.Net MVC and Web Services

I have an existing Asp.Net MVC Website and I would also like to provide a Web Service from the same domain.
What is the best way to approach creating a web service in this scenerio?
Do I add to this project or...?
You should be able to add an WebService file directly to the MVC project.
Right click on solution and select add new item, then select the web category and att the bottom of the list there should be Web Service.
Just remember to check that the routes does not eat up the call to the webservice.
That way the webservice can get access to the same model classes as the MVC application.
You can add a web service to the project just as you do in regular ASP.NET web apps, however, MVC basically IS a web service. You could create a controller that handles all the requests that you want your web service to handle.
With the advent of MVC it is quite common to do applications that only ever load a view once, then use AJAX and client scripting almost the entire rest of the life of the application. Your AJAX calls just hit up action methods for their goods and then use the deliciousness that is JSON to parse the data and utilize it.
In my opinion designing a web service as a controller instead of using [WebMethods] is far simpler and a lot more fun!
First, the question is "what do we mean by web service?" This can mean anything from a MVC page that responds using XML, JSON or some other agreed upon format to full blown SOAP and WS-* encumbered nightmares.
Anyhow, perhaps the best place to start is the WCF restful services -- these play very nicely with MVC, including routing.
The cool kids are using openrasta.

ASP.NET MVC & Web Services

Does adding a Web Service to my ASP.NET MVC project break the whole concept of MVC?
That Web Service (WCF) depends on the Model layer from my MVC project to communicate with the back-end (so it looks to me like it needs to be part of the MVC solution).
Should I add this to the Controller or Model layer?
It sounds like you should split out your model into its own assembly and reference it from your MVC-application and WCF-application.
YourApp.Data -- Shared model and data access maybe
YourApp.Web -- If you want to share more across your web-apps
YourApp.Web.Mvc
YourApp.Web.WebService
If you want to do WebServices MVC-style maybe you should use MVC to build your own REST-application.
Is there a specific reason you need to add web services to your MVC application? Unless there is a specific reason you should use your controllers in a RESTful manner just as you would a RESTful web service.
Check out this post from Rob Connery for more information:
ASP.Net MVC: Using RESTful architecture
Separating the Model into it's own project is not breaking the "MVC" pattern. First off, it is just that -- a pattern. The intention of the MVC pattern is to clearly delineate between your data, the data handlers, and the presenters and the way you interface between them. The best way to do it is how Seb suggested:
YourApp.Data
YourApp.Web.Mvc
YourApp.Web.WebService
Something that might help you out is the MVC Storefront that Rob Conery put together. Go watch the video's here:
MVC Storefront Video Series
And if you want to look at the actual code in your browser to quickly see how he did it, go here:
MVC Storefront Codeplex Code Browser
I don't think separating the model into it's own assembly has any bearing on whether or not you're using MVC, you still have a model. Where it is is irrelevant surely?
I've had a go at doing this.
See my result at my blog
ps: I don't believe that this will break the MVC concept so long as you think that a web service is the model of a repository because all a web service does is returning a XML dump.
I have added web services to my application and it works well. I don't believe it violates MVC because it is an alternative interface to your model. MVC is not appropriate for web services because web services don't have a view.
Think of web services and databases as one in the same. Under this analogy, I think it makes sense to place your web service ingteractions where you place your database logic.

Resources