ASP.NET MVC - Routes - asp.net

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

Related

how to get action with specific url asp mvc

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.

How to set another page as default page in ASPNET BOILERPLATE MVC5?

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.

Redirect using routing in MVC

I have ASP.NET MVC application.
I want my application to redirect from
example.com/Register
to
example.com/Account/Register
How can I do it with routes? It makes little sense to me to make controller only for this one task
public class RegisterController : Controller
{
public ActionResult Index()
{
return RedirectToAction("Register", "Account");
}
}
You don't need a redirect. You need a custom route
Add this route first (above "Default")
routes.MapRoute(
"Register",
"Register",
new { controller = "Account", action = "Register" }
);
This solution will leave the user on URL example.com/Register, but instantiate Controller Account, execute ActionResult Register, and return View Account/Register.

Common controller, multiple areas - need routing?

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.

ASP.NET MVC3 dynamic routing

I'm having trouble finding the answer to this question anywhere.
I am in need of creating a form where the user can create a post and change the url to the post.
For example, if the default route is
http://www.domain.com/posts/[the-title-of-the-post]
The user can change this to
http://www.domain.com/[modified-title-of-the-post].
The [modified-title-of-the-post] can be anything the user would like to make it.
This means it is no longer tied to the title of the post and not only that, but the /posts/ is gone too.
I guess I should Also mention that this should be global, meaning the user should be able to change the url (as mentioned above) for other things on the sites like, /topics/ or /blog/
Any help would be greatly appreciated,
Thanks,
Hiva
You could create two routes in your global.asax. Something like this
routes.MapRoute("", "posts/{url}", new { controller = "Home", action = "Posts" });
routes.MapRoute("", "{url}", new { controller = "Home", action = "Posts" });
both of them point to HomeController and the action Posts
public ActionResult Posts(string url)
{
}
to handle every url you should consider to extend the RouteBase class
Something like that should do
public class CustomRouting : RouteBase
{
public override RouteData GetRouteData(HttpContextBase httpContext)
{
RouteData result = null;
string requestUrl = httpContext.Request.AppRelativeCurrentExecutionFilePath;
//Handle the request
//Compile the RouteData with your data
result = new RouteData(this, new MvcRouteHandler());
result.Values.Add("controller", "MyController");
result.Values.Add("action", "MyAction");
result.Values.Add("id", MyId);
}
}
return result;
}
public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
{
//I only need to handle outbound so here is ok
return null;
}
}
The in your global.asax you register your custom route handler
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.Add(new CustomRouting());
routes.MapRoute("Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional });
}

Resources