0
votes

When I am using $routeProvider with MVC.Net its not working.

I am using Write up Backend example for MVC.Net. Following is the route I am using

config(function($routeProvider) {
    $routeProvider.
      when('/', { controller: ListCtrl, templateUrl: 'home/listitem' }).
      when('/home', { controller: ListCtrl, templateUrl: 'home/listitem' }).
      when('/edit/:projectId', { controller: EditCtrl, templateUrl: '/home/detail' }).
      when('/new', { controller: CreateCtrl, templateUrl: '/home/detail' }).
      otherwise({redirectTo:'/'});
  });

but unfortunately it is not calling listitem or details.

3
I am not sure that you should use the APS.NET MVC views with AngularJS as AngularJS is meant to handle all the UI work. You should probbably use only MVC as the backend to retrieve your data as an API. And if it is available to you, I suggest that you have a look at AngularJS + ASP.NET Web API.jpmorin
I would like to handle only UI stuffs. I have created the partial view which I would like to call using angularJS. As I belive that the code given by me will check the URL and on basis of that it call templateUrl. But currently it only works with static html files. Is it only support html static files ?pixelbyaj

3 Answers

3
votes

This works fine. Setup your RouteConfig.cs as follows:

        // Will handle partial page view requests
        routes.MapRoute(
            name: "Partials",
            url: "partials/{controller}",
            defaults: new { controller="Home", action = "Index" }
        );

        // Will handle all other requests by sending it to the main controller
        routes.MapRoute(
            name: "Application",
            url: "{*url}",
            defaults: new { controller = "Main", action = "Index" }
        );

Then your angular config for routing like this:

 .config(['$routeProvider', '$locationProvider',
        function($routeProvider, $locationProvider) {
            $routeProvider
                .when('/', {
                    templateUrl: '/partials/home',
                    controller: 'HomeController'
                })
                .when('/home', {
                    templateUrl: '/partials/home',
                    controller: 'HomeController'
                })
                .when('/gallery', {
                    templateUrl: '/partials/gallery',
                    controller: 'GalleryController'
                });
                .when('/services', {
                    templateUrl: '/Services',
                    controller: 'ServicesController'
                });

            // NOTE: This part is very important
            $locationProvider.html5Mode(true);
        }
    ]);

And your MVC controllers:

public class MainController : Controller
{
    public ActionResult Index()
    {
        return View();
    }
}

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return PartialView("_Home");
    }
}

public class GalleryController : Controller
{
    public ActionResult Index()
    {
        return PartialView("_Gallery");
    }
}

Put your partial views in the shared folder.

Now, behind the scenes you can do everything like you normally would with an MVC controller and view, including razor and all that.

1
votes

I have also this problem before I try this solution in config file.

config file

myapp.config(['$routeProvider', '$locationProvider', function ($routeProvider, $locationProvider) {

// Specify the three simple routes ('/', '/About', and '/Contact')
$routeProvider.when('/', {
    templateUrl: '/Home/Home',
    controller: 'homeCtrl',
});
$routeProvider.when('/Login', {
    templateUrl: '/account/Index',
    controller: 'loginCtrl',
});
$routeProvider.when('/About', {
    templateUrl: '/Home/About',
    controller: 'aboutCtrl',
});
$routeProvider.when('/Contact', {
    templateUrl: '/Home/Contact',
    controller: 'contactCtrl'
});
$routeProvider.when('/First', {
    templateUrl: '/account/First',
    controller: 'firstCtrl'
});
$routeProvider.when('/Fullpage', {
    templateUrl: '/Home/Fullpage',
    controller: 'fullpageCtrl'
});
$routeProvider.otherwise({
    redirectTo: '/'
});

// Specify HTML5 mode (using the History APIs) or HashBang syntax.
//$locationProvider.html5Mode({ enabled: true, requireBase: false });
$locationProvider.html5Mode(false);}]);

index.cshtml file

<a class="brand" href="#/">Simple Routing</a>
    <div class="nav-collapse collapse">
        <ul class="nav">
            <li class="active"><a href="#/">Home</a></li>
            <li><a href="/#/About">About</a></li>
            <li><a href="/#/Contact">Contact</a></li>
            <li><a href="/#/Login">Login</a></li>
            <li><a href="/#/First">first</a></li>
            <li><a href="/#/Fullpage">fullpage</a></li>
        </ul>
    </div><!--/.nav-collapse -->
0
votes

As @jpmorin suggested, unless for some reason you need to dynamically generate views in a way that AngularJS can't do, the MVC views are superfluous. You just need static files to serve, and some data-binding for them. Look at BreezeJS as a solution to that.

If this doesn't satisfy you, you would need to present your MVC code for a better answer. I personally suggest the former solution.