c# - How to make ActionResult inaccessible via URL but is need to be called from ajax? -


grettings friends. have been researching site looong time , have not gotten satisfactory answer question.

this 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 ajax in 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 make actionresult eliminarlibro inaccessible via url (for example xxxx/home/eliminarlibro/0000532307) need called ajax?

done! amr elgarhy:

 public class ajaxonlyattribute : actionfilterattribute {     public override void onactionexecuting(actionexecutingcontext filtercontext)      {         if (!filtercontext.httpcontext.request.isajaxrequest())         {             filtercontext.result = new httpnotfoundresult();         }     } } 

and 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);         }      } 

Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -