POSTing to my ASP.NET MVC application from a third-party site

I am testing a payment provider (SagePay) and, as part of the process, their POST servers to my site and expect a response. I cannot get this to work using MVC.

I created a classic asp test response page and added it to my MVC application:

<%
Response.Buffer = True 
response.Clear()
response.contenttype="text/plain"
response.write "Status=OK" & vbCRLF
response.write "RedirectURL=http://www.redirectsomewhere.co.uk" & vbCRLF
response.End()
%>

This work is wonderful.

However, when I try to do the same with MVC, it does not work:

Controller:

[HttpPost]
public ActionResult TestCallback()
{
     return View();
}

View:

@{
    Response.Buffer = true;
    Response.Clear();
    Response.ContentType = "text/plain";
    Response.Write("Status=OK" + System.Environment.NewLine);
    Response.Write("RedirectURL=http://www.redirectsomewhere.co.uk" + System.Environment.NewLine);
    Response.End();
}

The error message is a common error of the payment provider, so this is not real help, but I narrowed the error down to the point where the page is displayed.

I can view both pages in order (I need to remove the HttpPost attribute from the MVC controller method for this), and both pages display identical data.

This is the MVC URL that the payment provider performs POSTing:

http://myipaddress/CA_UAT/Token/TestCallback

URL- ASP, :

http://myipaddress/CA_UAT/Token/TestCallback.asp

"Token" asp, URL- .

?

UPDATE

Firefox "Header Spy", :

Response HTTP/1.1 200 OK
Source: Response
HttpHeader:Server
Request:User-Agent Cookie
Response:Response Date Set-Cookie

.

+3
4

, . - . , .

[HttpPost]
public string TestCallback()
{
  string result = "Status=OK";
  result += System.Environment.NewLine;
  result += "RedirectURL=http://www.redirectsomewhere.co.uk";
  result += System.Environment.NewLine;
  return result;
}

, . ActionResult View, , , .

+1

, , , :

[HttpPost]
public ActionResult TestCallback()
{
  Response.Buffer = true;
  Response.Clear();
  Response.ContentType = "text/plain";
  Response.Write("Status=OK" + System.Environment.NewLine);
  Response.Write("RedirectURL=http://www.redirectsomewhere.co.uk" + System.Environment.NewLine);
  Response.Flush();
  return new EmptyResult();
}

EmptyResult , MVC .

0

Try it like this:

[HttpPost]
public ActionResult TestCallback()
{
    var sb = new StringBuilder();
    sb.AppendLine("Status=OK");
    sb.AppendLine("RedirectURL=http://www.redirectsomewhere.co.uk");
    return Content(sb.ToString(), "text/plain");
}

or more MVCish way:

View Model:

public class ResponseViewModel
{
    public string Status { get; set; }
    public string RedirectUrl { get; set; }
}

and then the result of the user action:

public class StatusActionResult : ContentResult
{
    private readonly ResponseModel _model;
    public StatusActionResult(ResponseModel model)
    {
        _model = model;
    }

    public override void ExecuteResult(ControllerContext context)
    {
        var response = context.HttpContext.Response;
        response.ContentType = "text/plain";
        response.Write(string.Format("Status={0}{1}", _model.Status, Environment.NewLine));
        response.Write(string.Format("RedirectURL={0}", _model.RedirectUrl));
    }
}

and finally, the action of your controller:

[HttpPost]
public ActionResult TestCallback()
{
    var model = new ResponseModel
    {
        Status = "OK",
        RedirectUrl = "http://www.redirectsomewhere.co.uk"
    };
    return new StatusActionResult(model);
}
0
source

I wonder if sagepay is expecting a file extension. I am doing some validation of the url on the side of the heir. Do you know if your action is triggered?

Also try adding a route that makes your mvc url look like "TestCallback.asp".

0
source

All Articles