DELETE method not working - c#

I have written a WebApi controller that contains the following method for deleting a client...
[HttpDelete]
public void DeleteClient(int id) {
// do stuff here
}
...and am trying to test it by using the following HTML on a web page...
<form method="DELETE" action="/api/ClientsXml/">
<table style="padding: 5px">
<tr>
<td>ID</td>
<td><input type="text" size="20" name="id" id="id" /></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" value="Send" /></td>
</tr>
</table>
</form>
However, the DeleteClient method is never called. It passes the request through to the GetClient method instead.
Anyone any idea why? I've tried all sorts of variations, but I just can't get the delete method called.

#DimitryS's answer is correct, but I thought I'd build on it a little.
HTML forms only allow GET and POST operations. This is current in the HTML 5 spec as well as the HTML < 4 spec.
Other HTTP methods are allowed when using XMLHttpRequest which is what underlies jQuery's ajax functionality. So a good option could be to use jQuery for your PUTs, DELETEs, and it should work in all major browsers (some discussion of that in this pretty definitive SO question: Are the PUT, DELETE, HEAD, etc methods available in most web browsers?).
Lastly, I'll say that if you are just using the form to test your API, then why not try a browser extension instead:
e.g.
POSTMan for chrome
RESTClient for firefox
There are many more, but most allow you to save a test suite, set different headers, and so forth.

Browsers usually can only perform GET or POST for the form elements (unless you are submitting it using AJAX).
You should change the form method to POST and add the following HTML element:
<input name="X-HTTP-Method-Override" type="hidden" value="DELETE" />
That is the way MVC allows to override HTTP methods for synchronous POSTs.
Edit: this post explains how to make the Web API support the same convention:
https://stackoverflow.com/a/13135848/395359

Related

Ajax helper tags documentation in Asp.net Core

Is there is any link for Ajax helper tags documentation in Asp.net Core. I am trying to learn ajax with asp.net core but i found no documentation for it.
In asp.net mvc we use #Ajax.Form and then uses AjaxOptions method for work on ajax. After many hours search i found this link.
https://dotnetthoughts.net/jquery-unobtrusive-ajax-helpers-in-aspnet-core/
In this link there is a way work with ajax in asp.net core.
I implement it in my project and successful.
Then i search for its documentation but i found nothing.
I want its documentation link.Please anybody help for its documentation
There are no server-side helpers, like #Ajax.Form, in ASP.NET Core. You could probably write your own tag helpers for similar features but I haven’t seen anyone do this. The general idea is to write actual JavaScript when you want to have client-side behavior. Hiding these things behind server-side magic is usually not the best idea.
jquery-ajax-unobtrusive is a JavaScript package that adds client-side behavior to look for various attributes in the final rendered page to add functionality on top of your standard forms. So this would be a fully JavaScript-based solution.
Unfortunately, there does not seem to be documentation about it. You can take a look at its source code to figure out what may or may not be possible.
jquery-ajax-unobtrusive documentation
From taking a quick look at the source (disclaimer: without testing the functionality myself), this seems to be the supported data attributes and available functionality of the package:
data-ajax="true" – To enable the functionality for a form.
data-ajax-update – Selector for the elements that are updated with the AJAX result, using the mode.
data-ajax-mode
data-ajax-mode="before" – Prepends the data to the element.
data-ajax-mode="after" – Appends the data to the element.
data-ajax-mode="replace-with" – Replaces the element with the data.
Otherwise sets the HTML content of the element to the data.
data-ajax-confirm – Message that is displayed to the user to confirm the form submission.
data-ajax-loading – Selector of element that is shown while loading.
data-ajax-loading-duration (default: 0) – Animation duration for show/hide of the loading element.
data-ajax-method – Allows overwriting the HTTP method for the AJAX request.
data-ajax-url – Allows overwriting the URL for the AJAX request.
data-ajax-cache – Set to other value than "true" to disable the jQuery AJAX cache parameter.
data-ajax-begin – Callback function before the request starts (arguments: xhr)
data-ajax-complete – Callback function when the request is completed (arguments: xhr, status)
data-ajax-success – Callback function when the request was successful (arguments: data, status, xhr)
data-ajax-failure – Callback function when the request failed (arguments: xhr, status, error)
The callback functions are the equivalent of jQuery’s beforeSend, complete, success, and failure. From how it looks, you can specify the callbacks using a JavaScript object path to the function.
For example data-ajax-success="foo.bar.onSuccess" will call the function foo.bar.onSuccess(), i.e. it will look for an object foo in the window, get its bar member, and call onSuccess on that.
https://github.com/Behrouz-Goudarzi/AjaxTagHelper
AjaxTagHelper
A simple solution to using links and ajax forms using Tag Helper in aspnet core
First, copy the AjaxTagHelper class from the Extensions folder to your project.
Second, copy the AjaxTagHelper.js file from js folder in wwwroot and add it to your project.
Then do the following: Open the viewImports file and add the following code
#using AjaxTagHelper.Extensions
#using AjaxTagHelper
#using AjaxTagHelper.Models
#addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
#addTagHelper *, AjaxTagHelper
To use Action Ajax, add the following code instead of the tag.
<ajax-action ajax-action="Delete" ajax-controller="Home" ajax-data-id="#Model.Id"
class="btn btn-danger btn-sm" ajax-success-func="SuccessDelete">
Delete
</ajax-action>
Use the following code to use AJAX to send the form to server.
<div class="row">
<form id="frmCreate" class="col-sm-9">
<div class="row">
<div class="col-sm-4 form-control">
<input placeholder="Enter Name" name="Name" class="input-group" type="text" />
</div>
<div class="col-sm-4 form-control">
<input placeholder="Enter Family" name="Family" class="input-group" type="text" />
</div>
<div class="col-sm-4 form-control">
<input placeholder="Enter Email#site.com " name="Email" class="input-group" type="email" />
</div>
</div>
</form>
<div class="col-sm-3">
<ajax-button ajax-controller="Home" ajax-action="Create" ajax-form-id="frmCreate" ajax-success-func="SuccessCreate"
class="btn btn-sm btn-success">
Create
</ajax-button>
</div>
</div>
Finally, add the scripts you need to view it, check the code below
<script>
function SuccessCreate(data) {
console.log(data);
$("#tbodyPerson").append(data);
}
function SuccessDelete(data) {
$("#tr" + data.id).fadeOut();
}
</script>
<script src="~/js/AjaxHelper.js"></script>
If you're looking for the old style Ajax helpers in Core, this Nuget package might help -
AspNetCore.Unobtrusive.Ajax
You can install it using -
PM> Install-Package AspNetCore.Unobtrusive.Ajax
This will enable you to use helpers like
#Html.AjaxActionLink()
#Html.AjaxBeginForm()
#Html.AjaxRouteLink()
Here's the github details. You can find more documentation in there.
Github Url to the project

ASP.NET - Using SagePay Form Method Within a Content Page

I have an ASP.NET site and need to post some hidden form fields to SagePay so that my customers can pay for goods. I am using the following method to do this:
<input type="hidden" name="VPSProtocol" value="2.23" />
<input type="hidden" name="Currency" value="gbp" />
<input type="hidden" name="TxType" value="PAYMENT" />
<input type="hidden" name="Vendor" value="myvendorname" />
<input type="hidden" runat="server" id="crypt" name="Crypt" value="#<encrypted string>" />
<asp:Button ID="Button1" runat="server" Text="Pay Now" PostBackUrl="https://live.sagepay.com/gateway/service/vspform-register.vsp"/>
Now, If I use this code in a standard ASP.NET form, this works fine - SagePay accepts the posted information and continues with the payment process. However, if I use the same code inside a content page with a master page, Sagepay displays the following error screen:
5030 : We could not process your message, please check your
integration settings or contact the support team.
It seems as if the hidden fields are losing their value because of the master page.
Could anyone tell me what could be happening here and if there is anything I can do to rectify the situation. I need to use the SagePay Form method and I need to use a masterpage.
I haven't used webforms for a while but from memory by default it changes the names of your elements based on their container to allow navigation and identification server side: MSDN documentation here.
This means that your posted values are not under the name you expect them to be.

How to fill a web form and submit it [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to simulate browser HTTP POST request and capture result in C#
Send POST request in C# like a web page does?
I'm making a test application in c# and I was wondering how can I fill a webform.
There is just 1 box I want to fill ,Here the details are:
<td>Your name: </td>
<td><input class="text" type="text" name="name" /></td></tr>
<tr><td colspan="2" align="center">
<input class="text" type="submit" name="submitBtn" value="Login" />
So basically I would like to take the text of a textBox(its the easy part :)) inside of my WinForms application and put it into the name field which is on the url address and then send a request or press the Login button.
Is there an easy way to do this?
Take a look at the (Http)WebRequest
How to: Send Data Using the WebRequest Class
How to: Request Data Using the WebRequest Class

Displaying error messages using C# with Razor

I'm new to C# and am trying to add some simple server side validation to my site. I've tried to Google this, but information is a little thing on the ground.
So for instance, if I values inside of a form like such :
<table>
<tr>
<td>FredF</td>
<td>Fred Flintstone</td>
<td><input type="checkbox" name="userId" value="#user.UserId" /></td>
</tr>
<tr>
<td>BarneyR</td>
<td>Barney Rubble</td>
<td><input type="checkbox" name="userId" value="#user.UserId" /></td>
</tr>
<tr>
<td>WilmaF</td>
<td>Wilma Flintstone</td>
<td><input type="checkbox" name="userId" value="#user.UserId" /></td>
</tr>
</table>​
And I want to verify that the user has ticked a checkbox, and if they haven't, I want a message to be displayed saying they must check the box.
What is best practice for doing this?
Assuming you're using MVC, best practice for server-side is to use a ViewModel with your form that has an attribute specifying if the property is required or not.
So it would be something like
class User
{
[Required]
public bool IsChecked{get;set;}
}
Then in your Controller Post Action you check Model.IsValid and re-show the form if not.
That depends on many factors, like are you using a model, are you using jquery and do you have client side validation enabled.
Read this article:
http://www.codeproject.com/Articles/344292/ASP-NET-MVC3-Razor-With-JQuery-For-Beginners
http://www.asp.net/mvc/tutorials/overview/creating-a-mvc-3-application-with-razor-and-unobtrusive-javascript

How do I read value from code behind on html page? Or any better way of doing that?

Solution: I end up creatting a WCF that accepts a get/post request, then place JQuery within the html page that retrieves the value and hands it off to the web service
I have a html page like below where I will be doing posting to a web site for registrations and my credentials are not suppose to show on the client side.
My question is how/what is the best way of reading the values from code behind or web service or any other way ?
<FORM NAME="web_form" ACTION="https://website.com/registration.php" METHOD="POST">
<TABLE WIDTH=961 BORDER=0 CELLPADDING=2 CELLSPACING=0>
<TR>
<TH WIDTH=380>
<P ALIGN=RIGHT><i>Encrypted Username:</i>
</P>
</TH>
<TD WIDTH=573>
<P><A NAME="username"></A><INPUT TYPE=TEXT NAME="username" VALUE="HOW TO GET VALUE???" SIZE=20 STYLE="width: 1.69in; height: 0.23in"></P>
</TD>
</TR>
<TR>
..............
.................
Switch the input box in to an ASP control?
<asp:TextBox runat="server" ID="username" ClientIDMode="Static" />
Then in your code behind:
this.username.Text
There's probably a little more that we need to know to make a perfect suggestion, but there's tons of different ways that you could do this.
If you have access to the registration page -- which would be ideal -- you could send a base64encoded string via the URL or via hidden input in the form field posted on submit to the reg page and then base64 decode it before passing in. Similarly, you could salt the value with any number of different methods and "un-salt" it at the application.
Merely hiding it in a hidden input field probably won't suffice here as that code is certainly available on a view source, unless the data inside it is sufficiently obfuscated.
There are javascript obfuscators that will do a sufficient job, but they'll be unavailable if the user has javascript turned off. It won't affect 99% of the users out there, but it is something to think about.
Perhaps you could set this up as a dynamic page (not HTML) and set some sort of a constant? Perhaps a session variable that you then call in at the registration page?
You can do it on client side with JavaScript:
INPUT TYPE="text" NAME="username" VALUE="some value" onclick="CopyValue()" />
function CopyValue()
{
var myValue = document.getElementById('username').value;
}
Not an answer but just a suggestion. In HTML always try to keep your values in quotes. Always a good programming practice. Will definitely help you if you are trying to move from HTML to XHTML or XML. Good Luck!

Categories

Resources