Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a problem when combining angularjs with asp.net mvc. I am rendering html partial views inside ng-view directive using angular routing. And here i am omitting the angularjs default #(hash) navigation behavior using angular $locationProvider. Everything works fine, i can navigate to each partial view properly and every $scope objects also working fine. But there is a problem. $http.post method does not call to aps.net controller action. It does nothing. The debugging point does not hit. But $http.get works perfectly and it can retrieve data as json objects. But $http.post working well outside the ng-view. What i am missing here. Please help.

asp.net RouteConfig

public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "RoutesDemo",
                url: "{*catchall}",
                defaults: new { controller = "Home", action = "Index" });

            routes.MapMvcAttributeRoutes();

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

Home/index.chtml

<html ng-app="MyApp" ng-controller="MyController">
<head>
    <meta charset="utf-8">
    <base href="/">
</head>
<body>
    <h1>{{heading}}</h1>

    <ul>
        <li><a href="/routeOne">Route One</a></li>
        <li><a href="/routeTwo">Route Two</a></li>
        <li><a href="/routeThree">Route Three</a></li>
    </ul>

    <div ng-view></div>

    <script src="~/Scripts/angular.js"></script>
    <script src="~/Scripts/angular-route.js"></script>
    <script src="~/Scripts/app.js"></script>
</body>
</html>

one.html

<h3>One</h3>

<br />

<input type="text" ng-model="item" />
<input type="button" ng-click="postitem()" value="Send"/>

two.html

<h3>Two</h3>

three.html

<h3>Three</h3>

app.js

var app = angular.module('MyApp', ['ngRoute']);

app.config(function ($routeProvider,$locationProvider) {

        $routeProvider
            .when('/routeOne', {
                templateUrl: 'partials/one.html',
                controller: 'MyController'
            })
            .when('/routeTwo', {
                templateUrl: 'partials/two.html',
                controller: 'MyController'
            })
            .when('/routeThree', {
                templateUrl: 'partials/three.html',
                controller: 'MyController'
            });

        $locationProvider.html5Mode(true);
    });

app.controller('MyController', function ($scope,$http) {
    $scope.heading = "Page Heading";
    $scope.item = "Mango";

    $scope.postitem = function () {
        $http.post('/RoutesDemo/addMe', { foo : 'bar' }).success(function (response) {
        });
    }
})

RoutesDemoController

    public class RoutesDemoController : Controller
    {
        [HttpPost]
        public void addMe(string foo)
        {
            string fruit = foo;
        }
    }
share|improve this question
    
try to add HttpPost attribute for method in controller. –  rnofenko May 18 at 20:23
    
@rnofenko Not working –  Daybreaker May 18 at 20:29
    
What does the browser show for the network response on POST /RoutesDemo/addMe –  Jasen May 18 at 20:45
    
@Jasen response is my index.cshtml code –  Daybreaker May 18 at 20:55

1 Answer 1

I found where the problem is but it does not solved my problem fully instead it made a new problem. I made below changes to my code.

removed these lines form asp.net RouteConfig.cs

routes.MapRoute(
                name: "RoutesDemo",
                url: "{*catchall}",
                defaults: new { controller = "Home", action = "Index" });

            routes.MapMvcAttributeRoutes();

and changed navigation link in index.cshml

<li><a href="/#/routeOne">Route One</a></li>
<li><a href="/#/routeTwo">Route Two</a></li>
<li><a href="/#/routeThree">Route Three</a></li>

and also removed $locationProvider dependency.

app.config(function ($routeProvider) {

        $routeProvider
            .when('/routeOne', {
                templateUrl: 'partials/one.html',
                controller: 'MyController'
            })
            .when('/routeTwo', {
                templateUrl: 'partials/two.html',
                controller: 'MyController'
            })
            .when('/routeThree', {
                templateUrl: 'partials/three.html',
                controller: 'MyController'
            });
    });

Though it works fine. It recreated that #(hash) navigation problem. How to avoid that. I have to use /# in the url to havigate pages.

share|improve this answer
    
docs.angularjs.org/guide/$location html5mode –  Ric May 18 at 21:32
    
@Ric previous method i used html5mode and it worked well. but then $http.post method didn't work. I think the problem is the asp.net routeconfig. any idea how to change it for my need.?? –  Daybreaker May 19 at 3:50
    
It may not have been working as you have a catch all route. What was happening in the post? You say returning index.html?? –  Ric May 19 at 6:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.