Jump to content
AJBM

[Resolvido] Problema com rotas em ASP.NET e Angular

Recommended Posts

AJBM

Boas!

Eu a fazer um pequeno exercício com angular e ASP.NET MVC, estou a tentar aplicar o principio deste video

https://www.youtube.com/embed/f67PFtrldGQ?feature=oembed

Eu tenho este controller.

public class CustomerController : Controller
{
	// GET: Customer
	public ActionResult Index()
	{
		return View();
	}

}

Estas são as rotas que defini com angular.

var customerApp = angular.module("CustomerApp", ["ngRoute"]);
customerApp.config(['$routeProvider', '$locationProvider',
 function ($routeProvider, $locationProvider) {
  $routeProvider.
	when('/Customer', {
		templateUrl: '/App/Customer/Views/CustomersList.html'
	}).
	 when('/Customer/:id', {
		 templateUrl: '/App/Customer/Views/CustomersDetails.html'
	 }).
	otherwise({
		redirectTo: '/Customer'
	});
  $locationProvider.html5Mode(true);
 }]);

View principal

<!DOCTYPE html>
<html ng-app="CustomerApp">
<head>
<meta name="viewport" content="width=device-width" />
<title></title>
<base href="/" />
</head>
<body>
<h1>Customer</h1>
<div ng-view></div>
<script src="~/Scripts/angular.js" type="text/javascript"></script>
<script src="~/Scripts/angular-route.js" type="text/javascript"></script>
<script src="~/App/Customer/CustomerApp.js" type="text/javascript"></script>
</body>
</html>

Lista de Clientes

Eu clico neste link e funciona.

<h2>Lista</h2>
<a href="/Customer/1">Customer 1</a>

Mas quando eu estou na view /Customer/1 se fizer F5, como esta rota não está definida do lado do servidor dá erro.

Existe alguma maneira contornar este problema, sem ter de adicionar um novo método ao CustomerController ?

Share this post


Link to post
Share on other sites
AJBM

Modifiquei o ficheiro RouteConfig

  routes.MapRoute(
		 name: "Default",
		 url: "{controller}/{*catchall}",
		 defaults: new { controller = "Main", action = "Index", id = String.Empty }
	 );

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.