I have a controller defined in a library. I'd like this controller to be accessible via any of my 3 areas. At the moment, the controller (let's say "contact") is not being found when accessed via for example the "admin" area (i.e. url of /admin/contact). It does however work when accessed via "/contact".
Is there any route configuration required to Areas in order to allow the access of a common controller though these areas?
Thanks.
You could put this controller in a namespace:
namespace MvcApplication1.Controllers.MyAreas
{
public class ContactsController : Controller
{
...
}
}
and then in your area registration specify this namespace:
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional },
new[] { "SomeLibrary.Controllers.MyAreas" }
);
}
Now when you navigate to /admin/contacts/index the Index action of the ContactsController will be executed.
Related
i want get specific link to receive an action
this my controller :
namespace tabi.Controllers
{
public class CategoryController : Controller
{
public ActionResult List(string name=null)
{
ViewBag.name = name;
return View();
}
}
}
how to get action with this link :
/category/game
game is name parameter value
and don't change default route
If you haven't defined a custom route, you have to use the following url:
/Category/List?name=game
If you specify a custom route to allow List as the default action, and /{name} to the route (rather than ID), it would utilize the route you specified, such as:
routes.MapRoute(
name: "game",
url: "{controller}/{name}",
defaults: new { controller = "Home", action = "List" });
This route should support that URL.
I'm trying change default page when I start my app, but I can't do it. When I start the first page is "Account/Login", but I need it changes to other pages.
In project web I'm doing this: * HomeController: Add HomePage actionResult * View/Home: Add View to Home with name HomePage
In app_start/routeconfig.cs
routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "HomePage", id = UrlParameter.Optional } );
Also, i changed the properties of web project to specify page: Home/Homepage, but it's not working
Results in both options arent work
Template: Boilerplate with ASP.NET MVC 5 + Zero Module
i'm new with abp.
In HomeController.cs, comment out (or remove) the [AbpMvcAuthorize] attribute:
// [AbpMvcAuthorize]
public class HomeController : AbpProjectNameControllerBase
You can apply [AllowAnonymous] for specific actions to suppress authentication/authorization:
[AbpMvcAuthorize]
public class HomeController : AbpProjectNameControllerBase
{
public ActionResult Index()
{
return View();
}
[AllowAnonymous]
public ActionResult HomePage()
{
return View();
}
}
See the documentation on MVC Controllers.
Maybe it's better to use a different controller for anonymous actions. Create a new controller called WelcomeController. Do not add a AbpMvcAuthroize attribute. Then set your default route as Welcome/Index.
I am developing an ASP.NET MVC application. Content is divided into several areas:
Scheme - for shared functionality like errors
Pages - for displayed content
Data - for controllers, that returns JSON data
I don't have any controllers not assigned to areas.
I would like to access Pages controllers without typing area's name, and other to be accessed with their area names in route.
So I want to display action Users in Administration page under host/Administration/Users
And I want to display DatabaseTimeout Action from Error controller under host/Scheme/Error/DatabaseTimeout.
Analogously I want Create action from Codes controller from Data area under host/Data/Codes/Create.
Now, the problem: Pages area works as expected, Data area works as expected, Scheme area doesn't work as expected. When typing host/Scheme/Error/DatabaseTimeout application returns a 404.
Can anyone tell me what is wrong?
Here is part of application code:
Global.asax:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
RouteConfig.cs
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
}
DataAreaRegistration.cs
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Data_default",
"Data/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "MyWebApplication.Areas.Data.Controllers" }
);
}
PagesAreaRegistration.cs:
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Pages_default",
"{controller}/{action}/{id}",
new { controller="Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "MyWebApplication.Areas.Pages.Controllers" }
);
}
SchemeAreaRegistration.cs:
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Scheme_default",
"Scheme/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "MyWebApplication.Areas.Scheme.Controllers" }
);
}
AdministrationController.cs from Pages area:
namespace MyWebApplication.Areas.Pages.Controllers
{
public class AdministrationController : Controller
{
// action methods...
}
}
CodesController.cs from Data area:
namespace MyWebApplication.Areas.GridData.Controllers
{
public class CountryCodesController : Controller
{
// action methods...
}
}
ErrorController.cs from Scheme area:
namespace MyWebApplication.Areas.Scheme.Controllers
{
public class ErrorController : Controller
{
// action methods...
}
}
EDIT:
Ok, now I am really confused.
So under Scheme area I have, among others:
- _Layout.cshtml file, wchich is master page for every showed page
- Menu.cshtml, which is view for Menu action and is rendered on _Layout
Menu view is rendered by:
#{Html.RenderAction("Index", "Menu", new { area="Scheme" });}
Menu is rendered fine, however...
In menu there is many action links, that directs into different pages, written like this:
#Html.ActionLink("Manage users", "Users", "Administration", new { area = "Pages" }, null)
And that gives route like this: host/Scheme/Administration/Users
Eventhough area was specified. I tried to change area parameter into area="", did't help.
Please help me, I am confused and I cant go further with my work. :(
When you type host/Scheme/Error/DatabaseTimeout probably application returns a 404 because you don't have a action DatabaseTimeout in ErrorController.cs in Scheme area:
namespace MyWebApplication.Areas.Scheme.Controllers
{
public class ErrorController : Controller
{
public ActionResult DatabaseTimeout()
{
return View();
}
}
}
You must create the view ,where you show the error at the user, in your project under Areas\Scheme\Views\Error\DatabaseTimeout.cshtml.
The other question --> i think that if you want create a link to action Index() in HomeController under Schemearea you must use this in the views:
#Html.ActionLink("Home", "Index", "Home", new { area = "Scheme" }, null)
I'm trying to understand how the route config works in the MVC 5.
I have the following structure for my application:
BaseCRUDController
public class BaseCRUDController<TEntity, TEntityViewModel> : Controller
where TEntity : class
where TEntityViewModel : class
{
private readonly IBaseService<TEntity> baseService;
public BaseCRUDController(IBaseService<TEntity> service)
{
this.baseService = service;
}
[HttpGet]
public virtual ActionResult Index()
{
IList<TEntityViewModel> entities = baseService.FindFirst(10).To<IList<TEntityViewModel>>();
return View(entities);
}
}
CountriesController
[RouteArea("management")]
[RoutePrefix("countries")]
public class CountriesController : BaseCRUDController<Country, CountryViewModel>
{
private readonly ICountryService service;
public CountriesController(ICountryService service)
: base(service)
{
this.service = service;
}
}
What I'm trying to do is simple: http://myapplication.com/management/countries.
I have many others controllers that the superclass is the base controller. I'm doing this way to avoid code repetition, since that the controllers have similar structure.
The problems are:
I can't reach the url that I want (/management/countries)
I don't know how to configure my Home Controller, because I want that it could be reached by http://myapplication.com
How could I fix these problems?
My route config is like that:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
After a little playing around I managed to get it to work.
The problem is when you are using attribute routing there are no default route parameters assigned.
For example in your Default route the defaults are controller = "Home", action = "Index". So if you were to call http://myapplication.com/ the routing engine will automatically default to /Home/Index/ and so on.
However the attribute route has no way of knowing you want to default to the Index action (or any other action for that matter).
To solve the issue add the Route attribute to the CountriesController like this:
[RouteArea("management")]
[RoutePrefix("countries")]
[Route("{action=Index}")] // this defines the default action as Index
public class CountriesController : BaseCRUDController<Country, CountryViewModel>
{
private readonly ICountryService service;
public CountriesController(ICountryService service)
: base(service)
{
this.service = service;
}
}
Also for future reference Phil Haack's Route Debugger is extremely helpful for figuring out routing issues.
Have you defined Area (management) for your controllers?
if you try without Area, is that getting redirect properly?
I'm working on an MVC application and I have and admin area... So what I need is:
When user makes request to admin (for example "/Admin/Post/Add") I need to map this to controller AdminPost and action Add... is it possible?
If your controller is named AdminPostController and you want it to map to '/Admin/Post/Add' then you can use:
routes.MapRoute("Admin", // Route name
"Admin/Post/{action}/{id}", // URL with parameters
new { controller = "AdminPost", action = "Add", id = "" } // Parameter defaults
);
Note the use of the parameter defaults.
If your controller is named AdminController and you just wanted to separate the request method then use the default:
routes.MapRoute("Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
Which will map '/Admin/Add/' to the controller:
public class AdminController : Controller {
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Add(int id) {
//...
}
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Add(int id) {
//...
}
}
Note the use of [AcceptVerbs] to identify which method to invoke for POST requests and GET requests.
See Scott Gu's blog for more details