0

Grettings friends. So i have been researching this site for a looong time and i have not gotten a satisfactory answer to my question.

This is my Controller:

public ActionResult EliminarLibro(string bookId)
{
    bookModel ModeloLibro = new bookModel();
    ModeloLibro.EliminarLibro(bookId);
    TempData["message"] = "Se ha eliminado el libro correctamente.";
    return RedirectToAction("GestionBiblioteca");
}

And this is my Ajax in a view:

var myBookId = $('.parrafo-codigo').text();

    $.ajax({
        type: "GET",
        url: "/Home/VerificarEliminarLibro",
        data: { bookId: myBookId },
        datatype: "json",
        success: function (data) {
            // $('#result').html(data);
            if (data.esteLibroEstaPrestado == true) {
                $('#delModal').modal('hide'); // Quito modal de pregunta si se elimina
                $('#errModal').modal('show'); // Muestra modal de error
            } else {
                window.location.href = '/Home/EliminarLibro/' + myBookId;
            }
        }
    });

The question is: how to make ActionResult EliminarLibro inaccessible via URL (for example XXXX/Home/EliminarLibro/0000532307) but is need to be called from ajax?

Richard Ev
  • 52,939
  • 59
  • 191
  • 278
Ali Briceño
  • 1,096
  • 2
  • 17
  • 44
  • 1
    Check these: http://stackoverflow.com/a/6559042/20126 http://stackoverflow.com/a/9534353/20126 – Amr Elgarhy Nov 11 '14 at 14:05
  • good answer here http://stackoverflow.com/questions/4168341/asp-net-mvc-enforce-ajax-request-on-an-action – Joe Nov 11 '14 at 14:06
  • Pretty sure you can get the type of call made in the `ActionResult` and then from there you can decide what to do. But to be honest it doesn't make sense trying to do what you are doing. A Url is meant to be executed whether via ajax or not. Just had a thought, you could get the AJAX to send a key and then evaluate that key in the `ActionResult`, reject an invalid key – Callum Linington Nov 11 '14 at 14:07
  • @No1_Melman can you give me an example of this? – Ali Briceño Nov 11 '14 at 14:38

2 Answers2

0

Done! Thanks to Amr ElGarhy:

 public class AjaxOnlyAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)

    {
        if (!filterContext.HttpContext.Request.IsAjaxRequest())
        {
            filterContext.Result = new HttpNotFoundResult();
        }
    }
}

And the Controller:

 [AjaxOnly]
    public JsonResult VerificarEliminarLibro(string bookId)
    {
        bookModel ModeloLibro = new bookModel();
        bool HayLibrosPrestados = ModeloLibro.VerificarLibroPrestado(bookId);

        if (HayLibrosPrestados == true)
        {
            return Json(new { esteLibroEstaPrestado = true }, JsonRequestBehavior.AllowGet);
        }
        else
        {
            return Json(new { esteLibroEstaPrestado = false }, JsonRequestBehavior.AllowGet);
        }

    }
Ali Briceño
  • 1,096
  • 2
  • 17
  • 44
0
if (Request.AjaxRequest())
{
    // The Code
}
else
    throw new HttpException(404, "");
Arad Alvand
  • 8,607
  • 10
  • 51
  • 71