PATCH request with form-date c# - c#

In my web application I need to implement PATCH API endpoint to update/patch some entity.
I Know JSON Patch is something I can implement ,but problem here that the data from front end is in From-data format .Is there any way we can implement JsonPatch type process in form data for patch calls??

[HttpPatch("update")]
public Person Patch([FromBody]JsonPatchDocument<Person> personPatch)
{
personPatch.ApplyTo(_defaultPerson);
return _defaultPerson;
}
You need to change some configuration below link help to you
https://learn.microsoft.com/en-us/aspnet/core/web-api/jsonpatch?view=aspnetcore-3.1

Related

How can I connect my front end to my individually created backend

I am a beginner in creating a code and I am trying to code using vue js for my front end and aspnet core for my backend (web api). There arent many references in creating a to do list with this two connected.
I have already individually set up my front end in vs code while my back end in c#. I am having a hard time in connecting this two as there is not much update reference. How can I call my backend and connect it to my vue js.
thank you!
The first thing you need to do is open the back end CORS settings.
It connects to an endpoint on the Vue side, you can use axios framework to pull data
Here is an example
https://www.freecodecamp.org/news/how-to-build-an-spa-with-vuejs-and-c-using-net-core/
All you need to do is call the backend via API. Get the data via GET method, send the data via POST method. Let's say you want to get person list from the backend. You can do this -
const response = await fetch('https://yourawesomebackend.com/api/person/getpeople');
const content = await response.json();
// Now manipulate the DOM with the response.
In your backend, you might want to do this -
[Route("api/[controller]/[action]")]
[ApiController]
public class PersonController : ControllerBase
{
public IActionResult GetPeople()
{
// read people from database
var people = getFromDatabase();
return Ok(people);
}
}
Hope this helps.

How to check data is submitted in form, or in query string in ASP.NET Core Razor Pages?

I have a handler in my ASP.NET Core Razor Page:
public IActionResult OnPostDoSomething()
{
// How can I know that the data is sent in query string, or in form, or in headers?
}
This method is called from multiple places, and each calls it differently. One via a form, the other via an AJAX call and sends parameters in query string.
I know I can check if (Request.Query["key"].Count > 0) and based on this understand which method has been used.
But I wonder if ASP.NET Core Razor Pages has a built-in technology to let me know that a form
exists or not. For example something like if (Request.FormExists).
The reason I'm looking for a dynamic solution, is because I'm building a dynamic code and I don't have access to the name of the parameters being sent.
Is there a way for that?
Good,
you can always use the snippet context.Request.HasFormContentType, to check if there is a form attached to the request.
But I wonder if ASP.NET Core Razor Pages has a built-in technology to let me know that a form exists or not.
If you want to know if the request is sent by form,you can try to check content-type in headers:
if (HttpContext.Request.Headers["content-type"].ToString().Equals("application/x-www-form-urlencoded")) {
//request is sent by a form
}

Get CName in Code-Behind C#

I have a generic website called portal.com.
For each client, I will generate a custom website using CName, such as:
client1.portal.com
client2.portal.com
client3.portal.com
All will be redirected to portal.com. Arriving at something.portal.com website, I would like to take something to perform a custom action.
Does anyone know how to get this CName?
Use this if you just want the domain
HttpContext.Current.Request.Url.Host
If you want the port number too, you can use
HttpContext.Current.Request.Uri.Authority

Magento Change Order Status from REST API

I am 'communicating' with a Magento web app(version 1.9.2.2) via the REST API in a C# ASP.NET MVC application.
The application essentially acts as a backend order flow dashboard for pizzas. I need to display the latest orders and allow the user to check the items off as they are processed (among other things).
I am able to retrieve orders, products, customers etc; but need to be able to update the order status. From my research it seems that this can be achieved by adding an order comment.
That said, my questions are as follows:
Is adding an order comment (thus updating the order status) only possible through the SOAP Service in Magento 1.9?
If the above is true, how can I update the order status of a particular order using another secure approach?
Docs on REST API: http://devdocs.magento.com/guides/m1x/api/rest/Resources/Orders/order_comments.html
To anyone that may be facing the same issue, I discovered that it is not possible to update the order status (AKA add a sales order comment) via the REST API. You have to use the SOAP API and version 2 makes it easiest.
Setup:
In magento, create a SOAP Role and User
Add the SOAP v2 API as a web reference to your Visual Studio project
Code:
public void UpdateOrderStatus(string orderIncrementId, string newStatus, string comment = "")
{
// Init service with uri
var service = new MagentoSoap.MagentoService();
// Login - username and password (soap api key) of the soap user
string sessionId = service.login(Username, Password);
// Update order status
service.salesOrderAddComment(sessionId, orderIncrementId, newStatus, comment, 1, true);
}
You can do this by using the addComment method, which also lets you specify the new order status as one of it's parameters.
$sku='100000003';
$orderStatus = 'Downloaded';
$comment = 'The order was successfully downloaded';
$sendEmailToCustomer = false;
$proxy->call($sessionId, 'sales_order.addComment', array($sku, $orderStatus, $comment, $sendEmailToCustomer));

Any downsides to replacing REST endpoints with SignalR?

I'm building a fairly simple single page app. It's basically a list of items, where each item has some details, an activity log, and a current status along with some buttons to trigger actions on the server to advance the status along a workflow.
It was originally written using MVC and REST/Web API but I got stuck on the problem of keeping concurrent users up to date. For example, if User A adds an item, we want the list on User B's screen to now update to include it.
To solve this I looked into SignalR which works great. But I had a problem.
When adding an item (using POST) the callback adds the item on the requesting client. This is fine.
I then triggered a SignalR broadcast on the server to tell all clients about the new item. This worked fine except the local client, who now has 2 items.
I was looking into filtering the duplicate id client-side, or sending the connection id with the POST, then broadcast to all clients except the requester but it seems a bit needlessly complicated.
Instead I'm just doing this.
public class UpdateHub : Hub
{
public void AddNewItem(NewItem item)
{
// and some server-side stuff, persist in the data store, etc
item.trackingID = new Guid();
item.addLogEntry("new item");
// ...
dataStore.addItem(item);
// send message type and data payload
Clients.All.broadcastMessage("add", item);
}
}
It seems a lot simpler to just get rid of all the REST stuff altogether, so am I missing anything important?
It'll run on an intranet for a handful of users using IE11+ and I guess we do lose some commonly-understood semantics around HTTP response codes for error handling, but I don't think that's a huge deal in this situation.
In order to solve duplicate you can try to use Clients.Others inside Hub class, or AllExcept(id) if you not in the Hub class.
Clients.Others.broadcastMessage("add", item);
In your case using SignalR shouldn`t have any downsides.

Categories