How to configure angular routes with asp.Net Mvc

0

I created a controller that calls the Index.cshtml and within Index.cshtml contains the <ng-view> directive that should render index.html but does not work.

Controller

public class ClienteController : Controller
    {
        private ClienteBO clienteBO;

        public ClienteController(ClienteBO clienteBo)
        {
            this.clienteBO = clienteBo;
        }
        //
        // GET: /Cliente/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult New()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Salvar(Cliente cliente)
        {
            var id = clienteBO.Salvar(cliente);

            return Json(new { id = id }, JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        public ActionResult Listar()
        {
            var clientes = clienteBO.ListarTodos();

            return Json(new { ListaClientes = clientes }, JsonRequestBehavior.AllowGet);
        }

        public ActionResult ListarPorNome()
        {
            throw new NotImplementedException();
        }

        public ActionResult Atualizar()
        {
            return View();
        }

        [HttpDelete]
        public ActionResult Deletar(int Id)
        {
            clienteBO.Excluir(Id);

            return Json(new { id = Id }, JsonRequestBehavior.AllowGet);
        }

    }  

JS

angular.module("modaFeminina", ["ui.materialize", "ui.mask", "ngRoute"])
    .config(function ($routeProvider, $locationProvider) {

        $routeProvider.when('/Cliente/', {
            templateUrl: '/AngularTemplates/Index.html',
            controller: 'ClienteController'
        });


        $routeProvider.when('/Cliente/Atualizar', {
            templateUrl: '/AngularTemplates/Atualizar.html',
            controller: 'ClienteController'
        });

        $routeProvider.when('/Cliente/Listar', {
            controller: 'ClienteController'
        });

        $routeProvider.otherwise({ redirectTo: "/ModaFeminina/Cliente" });
    });

View Index.cshtml

@{
    ViewBag.Title = "Atualizar";
}
<h3>Index</h3>
<div ng-controller="ClienteController">
    <ng-view></ng-view>
</div>

Index.html

<div class="row" ng-controller="ClienteController">
    <table class="col s12 m12 l12 striped">
        <thead>
            <tr>
                <th data-field="id">Nome</th>
                <th data-field="cpf">CPF</th>
                <th data-field="telefone">Telefone</th>
                <th data-field="celular">E-mail</th>
                <th data-field="celular">Ações</th>
            </tr>
        </thead>

        <tbody>
            <tr ng-repeat="cliente in clientes">
                <td>{{cliente.Nome}}</td>
                <td>{{cliente.Cpf}}</td>
                <td>{{cliente.Telefone.Celular}}</td>
                <td>{{cliente.Email}}</td>
                <td>
                    <a href="" class="btn-floating waves-effect waves-light blue" ng-click="editar(cliente)"><i class="material-icons prefix">mode_edit</i></a>
                    <a class=" btn-floating waves-effect waves-light red" ng-click="deletar(cliente)"><i class="material-icons prefix">delete_forever</i></a>
                </td>
            </tr>
        </tbody>
    </table>
</div>
    
asked by anonymous 05.07.2016 / 01:53

1 answer

0

Hello, from what I can understand of your question, you have created an "AngularTemplates" folder and can not access the htmls. This is for security reasons, only some folders "serve" static documents, you will find a good documentation of this in the

You have some outputs:

  • add the templates directory in the configuration as a directory serving static content;
  • put the templates into a folder that already serves static content (such as the "content" folder;
  • create a controller and an action that returns "ContentResult" and receive as a parameter the name of the html you need
05.07.2016 / 13:28