Reputation: 95
I'm getting started with ASP.Net core MVC 3.1 and have an area called Foo:
endpoints.MapAreaControllerRoute(
name: "Foo",
areaName: "Foo",
pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}"
);
And in this area a controller called Test with an Index action: https://localhost:44390/Foo/Test/
- this works fine.
I'm trying to redirect to this from another controller with
return RedirectToAction("Index", "Test", new { area = "Foo" });
but this is sending me to https://localhost:44390/Test?area=Foo
How can I use RedirectToAction()
to end up at https://localhost:44390/Foo/Test/
?
Upvotes: 4
Views: 1872
Reputation: 487
I think you should below code:
namespace Solution.Areas.ControlPanel.Controllers
{
[Area(nameof(Foo))]
[Route(nameof(Foo) + "/[controller]")]
public class HomeController : Controller
{
public IActionResult Index() => View();
}
}
Upvotes: 0
Reputation: 4279
You can use the following code
set route in Configure:
"{area:exists}/{controller=Home}/{action=Index}/{id?}"
then
public IActionResult Index()
{
return RedirectToAction("Index", "Test", new { area = "Foo" });
}
then
[Area("Foo")]
[Route("[controller]")]
public class TestController : Controller
{
public IActionResult Index()
{
return View();
}
}
Upvotes: 0
Reputation: 20106
I reproduce your problem when MapAreaControllerRoute
is placed after the default route.So, to resolve it, your area route config must go at first place.
app.UseEndpoints(endpoints =>
{
endpoints.MapAreaControllerRoute(
name: "Foo",
areaName: "Foo",
pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}"
);
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
Upvotes: 1
Reputation: 139
put these attributes in the Test Controller.
[Area(nameof(Foo))]
[Route(nameof(Foo) + "/[controller]")]
Upvotes: 0