Reputation: 19664
What am I doing wrong? The does not hit the action and the error is a 405 - >The HTTP verb POST used to access path "somepath" is not allowed.
Client Script
$.post('/DecisionPoint/ApplicationState', { fileName: fileName, stateString: e });
filename is just a 'string' as is 'e'
Controller Action
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult SaveApplicationState(string fileName, string stateString)
{
string filePath = GetDpFilePath(fileName);
HtmlDocument htmlDocument = new HtmlDocument();
htmlDocument.Load(filePath);
HtmlNode stateScriptNode =
htmlDocument.DocumentNode.SelectSingleNode("/html/head/script[@id ='applicationState']");
stateScriptNode.InnerHtml = "var applicationStateJSON =" + stateString;
htmlDocument.Save(filePath);
return Json("State Updated");
}
UPDATE
This is my global.asax.
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"DecisionPoint", // Route name
"{controller}/{action}/{fileName}/{stateString}", // URL with parameters
new { controller = "DecisionPoint", action = "ApplicationState", fileName = UrlParameter.Optional, stateString = UrlParameter.Optional} // Parameter defaults
);
The error is now - Resource not found. The script lives in the standard /Scripts folder that the template creates.
Upvotes: 1
Views: 4891
Reputation: 54854
Are you sure this is your path to the control and action?
Controllers/DecisionPoint/SaveApplicationState
Because normally the word "Controllers" doesn't show up in the URL path, and that is what jQuery needs the exact URL path for accessing this action.
Try just this:
string url = "/DecisionPoint/SaveApplicationState/" + filename + "/" + e;
jQuery.post(url);
The problem is you have mapped the route to an exact URL, so in order to post to that action, you need to recreate the URL. Also please note, I don't know if this is intentionally or not, but you action name is SaveApplicationState
however in the route mapping you have the action listed as ApplicationState
. This needs to be consistent.
Upvotes: 1
Reputation: 2159
It looks as though the url to which you are posting is not correct. In Asp.net MVC, you do not post directly to controllers, you post to a route that usually resolves to a controller. So, for example, if I had a controller called DecisionPointController
with an action (method) called ApplicationState
(I dropped the word "Save" to be more RESTful, then the corresponding url would be:
~/DecisionPoint/ApplicationState
.
Upvotes: 1