1

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.

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

That doesn't work because the code you just posted in the question changes the whole approach to the question.
1

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.

2 Comments

I have a taken your advice and changed the action to 'ApplicationState' I updated my question to show my global.asax file. I don't understand why the url is incorrect.. I changed it to '/Controller/Action' still no good
No worries! In the code above, you are attempting a POST, not a GET. By doing so, you are passing the values (fileName, and stateString) in the request, not in the url. This means you do not need a separate Route. Check out this post by Scott Guthrie for more information regarding basic routing and how Controllers work. If you remove the custom route, does it work? Also, just as an FYI, routes are matched in order, so always put custom routes above default routes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.