How to invoke HTTP PUT from URL? - c#

Scenario:-
I have a form, once user submits this form, a mail is sent to user.
As usual an activation link is present in the mail body.
And if the user click on the Link, he is redirected to the page,now on visit of the url, I want to update the field in my DB table :-
isEmailVerfied to true
DateOfVerification
I am using WebAPI and HTTPPUT action to update the database.
ControllerName is Registration, I don't want the user to click on any extra button to update, I want if the user visits the url, HTTPPut(or update) operation should be called and that links to be invalid, once user clicked.

I'm assuming the flow of actions in this scenario is something like this:
User fills out a form on a webpage
User submits form to your web api backend
The backend saves the data submitted from the form
The backend sends out an email containing a link to your application
User reads email and clicks on the link
And you want the action in step 5 here to trigger some other action in your backend, which is to update some data in your database which you have already implemented and have exposed as as a http PUT method in your api.
The problem is that clicking on a link from an email that opens up in a browser is that you cannot specify the http method. Navigating to URLs in a browser, which is what you are doing, is a GET request. So your PUT action will never get hit.
To resolve this you can just change your action from PUT to GET.
change this:
[HttpPut]
[Route("verify/{hash}")
public void VerifyEmail(string hash){
// your implementation logic
}
to this:
[HttpGet]
[Route("verify/{hash}")
public void VerifyEmail(string hash){
// your implementation logic
}

Related

Stripe Simple Checkout integration with cancel button

I am trying to integrate Stripe with my website with "Simple" checkout as described at https://stripe.com/docs/checkout . I have created a summary page on which I have added script tag. This shows the Pay With Card and it works fine.
However, I need a "Cancel" or "Back" button on this summary page so as to give user chance to go back to previous page or cancel online booking. But even when this other button is clicked, the Payment pop-up is opened and it is not raising the back button event.
What am I missing? Why even other buttons are hijacked by Stripe JS. Please help.
Simple Checkout allows the user to enter their credit card, then creates a token, then immediately submits the <form></form> which encloses it. If you need more customizability than that, you'll need to use the Custom Checkout integration.
With the Custom integration, the user is presented with Checkout, Stripe generates a token, then it is up to you to decide what to do next --- you could write JS in the token creation callback to append a hidden field with the token and then immediately submit the form, or you could land the user back on your summary page and wait for the user to give an additional confirmation before submitting your form, or 'go back.'
var handler = StripeCheckout.configure({
key: 'pk_test_xxxyyyyzz',
token: function(token) {
// You can access the token ID with `token.id`.
// Do something with that token (append a hidden input + submit the form?)
}
});

Calling a specific action of a controller on the click of HTML button(Not Submit or Form's post Button) Asp.net MVC

When we click a Form's submit button, then action of the controller which is having HTTPPost attribute is called but what if i want to call or perform an action when a normal HTML button is clicked
Although following articles
http://www.codeproject.com/Tips/198477/Calling-a-MVC-Controller-and-Action-Method-using-H
HTML button calling an MVC Controller and Action method
tells the approach but both of these are using controller name in view, So view has to know about controller, I am looking for an answer that view wont have to know about controller.
because views has to be Independent from Controller, Views should not know about controller
So, if you know the answer then please reply
any form that directs your user to url created by
<a href='#Url.Action("{action}", "{controller}")'> click me </a>
or
#using(BeginForm("{action}", "{controller}")
will do what you want.
That can be with a
form
button link
It's the destination that matters. The View does not "know" anything about the action or controller. The helper does.
To execute an MVC action from the client side (i.e. from a view) you need to hit a URL (with any verb: get, post, put, etc).
Therefore to execute an action form a view you will need to know the URL of that action, by default that URL is directly mapped to the controllername/actionname but you can re-define this if you want to create more abstraction between view and controller.
Given this your button just needs to be a link to a URL or linked to js to do an Ajax http request.
Hope that helps.
You cannot have 2 actions on the same controller with the same name and the same HTTP verb. So what you are asking doesn't make sense. You could invoke the same controller action as the one that rendered the view without specifying an action and controller name. The reason why Html.BeginForm() works without specifying an action and controller name is because the form is sending a POST request to the server and you can distinguish the 2 actions.

Browser Back Button issue in asp.net

I have registration form, suppose there are 5 fields, and all are required i.e. mandatory.
My problem is that when user login & fill 2 fields of that registration form & click browser back button, then I want to restrict user to same form & Show requiredField Validator messages of incomplete fields. So How can I fire Validation on browser back button & restrict user on same form.
You cannot affect browser behavior when the user clicks the Back button.
The correct solution is to check right after user have been login, if he has complete the registration entries on database, and if not you redirect him to fill that data.
From your comments, to alert them a solution is to capture the onbeforeunload and check there if they have fill out everything or not, show your message.
Simple example, you just need here to place your conditions.
window.onbeforeunload = function() {
return 'You have unsaved changes!';
}
You can't disable the back button (see the other answers and their comments).
What you can do is check on every page whether the user is logged in. If not, redirect him to your login page. This way the user can't access the site without logging in, which I guess is what your client is really after.

how to check in c# whether request is sent from fancybox or normal web page

i have two form in my sites but both are submitting on same action ,
i form open in fancy-box and one open in a normal page ,
you can i know that the request is made from fancy-box
or the noraml page , because if form submission fails due
to validation i have to send back the same view with validation
errors from which user made request .
public ActionResult SubtmitForm(contact model)
{
// here i will check whether request is from fancy-box form or from normal web page form
}
Note : i am using asp.net mvc3
There is nothing built in to say where a request originated, in particular not from how the browser requested it.
You need to build this into your model and populate the data in javascript (or otherwise).
Something like a public bool FromFancyBox { get; set;}; on the model with a HiddenFor on the view.
When opening the fancybox you set the value to true and when closing it, you set it to false (though there are other mechanisms).
In the controller, test for this property.

Identify sender in http module

I am developing a page tracker app where i need the login time,log out time ,session id,logged in user and each page accessed by user,time spent on that page and the list of event's in a page that are fired when controls are clicked,checked or selected.i dont want to use google analytics
For now i am accessing all the details by writing session start etc in http modules and everything is achieved.i am storing all the data in a hashtable and throwing them into db when logout is clicked or session time's out.i am struck on how to identify if logout button is pressed .
i can directely write the piece of code for inserting the statistics on the click of logout button itself but i dont want to do so as i want to capture the logout button id or text in http modules and then write the logic in http module.i tried to capture the sender but it's throwing an error by referring the asax reference instead of button reference
how can i acheieve this(track which button is clicked on a page and get the id in http module because the http module triggers for every postback or load)?
You could use your HttpContext.Request.Form object. It contains Key "__EVENTTARGET", which value equals to ID of object that sends POST to Server.

Categories

Resources