What is the Different between HttpPost and HttpPut ? in Web Api [duplicate] - http

This question already has answers here:
Why use HTTP PUT and DELETE methods instead of POST?
(4 answers)
Closed 4 years ago.
In CRUD Operations
Many People use [HttpPost] to ( Add, Update , Delete ) .
So : When we use [HttpPost] , [HttpPut] , [HttpDelete] and what is the Different .?

To make it simple :
POST = create
PUT = update (more precisely, full update. For a partial update, use PATCH)
DELETE = delete
More on that here : https://www.restapitutorial.com/lessons/httpmethods.html

Related

ASP.NET MVC - Using Data from another Controller in View [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
Newbie question on ASP.Net MVC: I have a project and I manage two "Models" - let's say "Products" and "Clients". Each Model has it's own Controller, and set of Views to implement the basic CRUD operations.
Now I want to list from a different View (lets say Home Page) all Products and all Clients.
Should I create new methods in Products and Clients Controlers to list their items and call these methods from the Index view from Home? Should the Home Controller call the Products and Clients methods?
How should I correctly address this?
Thansk in advance!
Pedro
The answer to this question is to some degree both subjective and opinion-based. That being said..
It is fine for HomeController to call Product and Client related methods to return what will effectively become your HomeIndexViewModel (which would comprise of some kind of ProductViewModel and ClientViewModel properties - your view would access the data via Model.ProductViewModel, Model.ClientViewModel, etc).
I would go a step further and add an orchestration component, which will be responsible for handling the request and application logic required by the Index action of your HomeController. This should keep your controller thin and decoupled from the backend.
public class HomeController : Controller
{
private readonly IOrchestrator orchestrator;
public HomeController() : this(IOrchestrator orchestrator)
{
}
public HomeController(IOrchestrator orchestrator)
{
this.orchestrator = orchestrator;
}
public async Task<ActionResult> Index()
{
var homeIndexViewModel = await orchestrator.GetHomeProductsAndClientsAsync();
return View(homeIndexViewModel);
}
}
GetHomeProductsAndClientsAsync() will obviously be a method of your FooOrchestrator class that will be handling the application logic to return a HomeIndexViewModel.
The orchestrator is passed in to HomeController as an interface (IOrchestrator, rather than the FooOrchestrator class which will implement the IOrchestrator interface) to facilitate dependency injection (though, IMHO, the approach is still beneficial without DI).
Some reading:
"Never Mind the Controller, Here is the Orchestrator" (Dino Esposito)

Symfony: Inject Service into User Object [duplicate]

This question already has answers here:
Symfony 2.0 getting service inside entity
(3 answers)
Closed 6 years ago.
How can I inject a service into the current user object?
With current user object I mean this:
//in controller
$user = $this->get('security.token_storage')->getToken()->getUser();
//later I want to do this in the user class:
function getData() {
$data = $this->getSomeData();//normal function
$data += $this->getMyService()->getSomeMoreData();//invoke service function
}
This is very bad practice as your Entity classes should be simple objects that are responsible only for manipulating their own data.
If, however, you are determined to do this you can create the container property in your User class and inject it in the Doctrine postLoad event, see the documentation.
I would stress again that you shouldn't do this though. I've seen this done before and it leads to over-complex classes that can have multiple responsibilities and are impossible to test.

How to handle custom sql functions on schema update? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
Sometimes when you're looking for performance, you need to delegate some responsibilities to the database with some kind of complexity functions and triggers. I'd like to know what is the best practice to handle those custom sql functions to create/update when doctrine:schema:update command is called.
The easier solution you have (I think) is to create your own command, do your logic inside, and call the doctrine:schema:update at end.
To do this, you can extend your command from the the UpdateSchemaDoctrineCommand or use a Process in your command.
I prefer the first solution, also I will show you.
Create the command in src/Acme/AppBundle/Command/CustomUpdateSchemaDoctrineCommand.php
(for example, use one of your own bundles)
Then, extend it from the parent command like this :
<?php
namespace Acme\AppBundle\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Doctrine\Bundle\DoctrineBundle\Command\Proxy\UpdateSchemaDoctrineCommand;
class CustomUpdateSchemaDoctrineCommand extends UpdateSchemaDoctrineCommand
{
protected function configure()
{
parent::configure();
$this->setName('custom:schema:update')
}
protected function execute(InputInterface $input, OutputInterface $output)
{
// Do your logic
// Update your database schema
return parent::execute($input, $output);
}
}
If you need a tool that allow you to run SQL migrations, use the DoctrineMigrationsBundle
I dont know way to realize automatically running other SQL-code, but you can use DoctrineMigration. It's run manualy, but you can write custom SQL and control versions - file contains creation date, used migrations names will be stored in DB.

How can i do a Count of Collection using Asp.net , Web API 2 and OData v4 [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I want to order results based on a count of collection using Asp.net, web api2 and OData v4.
My url is: url/odata/groupeclients?$expand=Client&$orderby=Client/$count
I get this error :
"The query specified in the URI is not valid. The parent value for a
property access of a property '$count' is not a single value. Property
access can only be applied to a single value."
Is this supported in Web API OData? If not, is there any alternative solution?
Regards,
Hayfa
It's not currently supported; there's an open issue.
As a workaround, you could define an OData function bound to groupeclients that explicitly performs the expansion and ordering. Something like:
[HttpGet]
public IHttpActionResult OrderByClientCount()
{
return this.Ok(data.Include(e => e.Client).OrderBy(e => e.Client.Count));
}
Note that this code is untested and may not even be possible if your IQueryable data source does not support Include (or its equivalent).

ASP.Net MVC case conventions [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I want to know which is the best convention for controller, action and view naming in ASP.Net MVC
When i create a "Product" Controller, should i name it productController or ProductController ?
By default, visual studio Create ProductController.
But that means the url will be http://mywebsite/Product/xxxx
Or we should not have upper case like this in URLs ?
So i do not know which convention apply.
This is the same for view names and action names...
you can use First latter capital as default MVC does, and for lower case url you can use RouteCollection.LowercaseUrls Property
check below link
https://msdn.microsoft.com/en-us/library/system.web.routing.routecollection.lowercaseurls.aspx
Example:
public static void RegisterRoutes(RouteCollection routes)
{
routes.LowercaseUrls = true;
}
Controller is just a class and should follow class capitalization rules, iow should use PascalCase: https://msdn.microsoft.com/en-us/library/x2dbyw72(v=vs.71).aspx
You can use StyleCop to check your code for such things as casing rules, spaces, etc: https://stylecop.codeplex.com/
But still nobody can prevent you from naming like 'productController'.
Also, if you wish to change appearance of controller name in URL, please check next question and corresponding answers (but usually people ten to use default naming): How to Change ASP.NET MVC Controller Name in URL?
In short, you can do this with RoutePrefixAttribute or by configuring routing (not shown here):
[RoutePrefix("product")]
public class ProductController
{
...
}

Resources