0

I have read almost every question on SO about Ajax Post to controllers. I have tried every single solution that I saw here. Nonetheless, my post still not working. Thus, please do not consider it as a duplicate, at least until I get an answer.

public class BlogController : Controller
    {
    [HttpPost]
    public ActionResult Test(BlogElement data)
    {
        return null;
    }

    public class BlogElement
    {
       public string HtmlContent { get; set; }
       public string Date { get; set; }
    }

And the Ajax part

var data_ = { HtmlContent: "someContent", Date: "someDate" }
    $.ajax({
        type: "POST",
        url: "/Blog/Test",
        dataType: "json",
        data:data_ 
    });

Ajax response error says "Not Found". Here is my route config. I am actually only using attribute routing.

routes.MapMvcAttributeRoutes();

What might be wrong with this configuration?

10
  • What do you have in console? F12 in browser Commented Nov 10, 2015 at 14:27
  • Or use Fiddler to see what the request looks like. Commented Nov 10, 2015 at 14:31
  • 2
    Define "not working". What's the status code of the response? Commented Nov 10, 2015 at 14:32
  • @haim770 Response says "not found" Commented Nov 10, 2015 at 14:39
  • Show your routing configuration Commented Nov 10, 2015 at 14:39

1 Answer 1

2

Apparently, calling MapMvcAttributeRoutes() in your RoutesConfig doesn't mean the default {controller}/{action} route will be registered as well.

Either add it:

routes.MapRoute(
   name: "Default",
   url: "{controller}/{action}/{id}",
   defaults: new { controller = "Home", action = "Index", Id = UrlParameter.Optional } );

Or decorate your action with the appropriate [Route] attribute:

[HttpPost]
[Route("Blog/Test")]
public ActionResult Test()
{
    // ...
}
Sign up to request clarification or add additional context in comments.

Comments

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.