MapMvcAttributeRoutes start page - asp.net-mvc

I have an attribute route for my login page. How can i set that page in the routeconfig to be the start page of my application?
My RouteConfig
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.LowercaseUrls = true;
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Login", action = "Login", id = UrlParameter.Optional }
);
}
My Controller:
[Route("Login")]
[HttpGet]
public ActionResult Index()
{
return View();
}
If I changed this
action = "Login"
To
action = "Index"
it's still doesn't work.

Related

.Net Route Action Parameter coming back null despite being in url

UPDATE: It works fine is I add another parameter. I have no idea why.
Not sure what I am doing wrong, this route looks the same as the other routes, I've looked at all the other stackoverflow posts on the subject and everything looks up to snuff. Maybe someone can see something I can't?
Error: action parameter can't be null
The url: http://localhost:1319/EntryHistory/Entry/B2AAAA4A-B174-4C28-924F-A3B2027DD745
EntryHistoryController.cs:
public class EntryHistoryController : Controller
{
public ActionResult Entry(string entryId)
{
Guid parsedEntryId = Guid.Parse(entryId);
var history = new EntryHistoryService().BuildEntryHistoryViewModel(parsedEntryId);
return View(history);
}
}
RouteConfig.cs
public static void RegisterRoutes(RouteCollection routes)
{
...
EntryHistoryRoutes(routes);
...
}
private static void EntryHistoryRoutes(RouteCollection routes)
{
routes.MapRoute(
"revisionhistory",
"entryhistory/entry/{entryId}",
new
{
controller = "entryhistory",
action = "entry",
entryId = UrlParameter.Optional
}
);
}
add attribute routing
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
...... your routes
//convention-based routes
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
and action
[Route("~/entryhistory/entry/{entryId}")]
public ActionResult Entry(string entryId)

ASP MVC routing rest api

i would like to create rest api but I have problem with routing.
This url is ok: localhost/project/controller/action/id, but i would like this url localhost/project/controller/id too.
For example: localhost/project/article/5: ArticleController.Get(int id)
This is my code:
Project controller
[HttpGet]
[Route("project/{id}")]
public JsonResult Get(int id)
{
}
Route config:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Api",
url: "{controller}/{id}",
defaults: new { id = UrlParameter.Optional }
);
}
When i call url localhost/project/article/5, error message is A public action method '5' was not found on controller 'ArticleController'.
Where is problem? Thanks
You can use attribute routing
Example:
[HttpGet]
[Route("project/{controller}/{action}/{id}")]
public JsonResult Get(int id)
{
}
OR
Default Route Table
Example:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "Api",
url: "project/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
As Per your Requirement, your Routing is:
[HttpGet]
[Route("project/article/{id:int}")]
public JsonResult Get(int id)
{
}
OR
routes.MapRoute(
name: "Api",
url: "project/article/{id}",
defaults: new { controller = "ArticleController", action = "Get", id = UrlParameter.Optional }
);

Dealing with multiple custom routes in MVC

I have my Home controller set up like this, going into different functions depending on the parameters it recieves.
Problem is in my home controller, it treats "gametwo" as a query for my route on my home controller.
Example
mysite.com/serchsomething <-- This will search the given string
mysite.com/gametwo <-- This also searches instead of going to gametwo controller
I have normal routeconfig.cs file, with just added attributeroutes.
What is the best way of dealing with routes with multiple parameters? So that they wont be ambigious or crash with any other routes? thanks
home controller
public ActionResult Index()
{
...
}
[HttpGet]
[Route("{Query}")]
public ActionResult Index(string Query)
{
...
}
[HttpGet]
[Route("{Query}/{Version}")]
public ActionResult Index(string Query, int Version)
{
...
}
GameTwo controller
[Route("GameTwo")]
public ActionResult Index()
{
return View();
}
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 }
);
}
Try this above
Home Controller
[HttpGet]
public ActionResult serchsomething(string Query)
{
//do something
}
Game Two Controller
public ActionResult Index()
{
return View();
}
Routing
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
/*serchsomething action*/
routes.MapRoute(
name: "Your route name 1",
url: "serchsomething/{Query}",
defaults: new
{
controller = "home",
action = "serchsomething"
}
);
/*GameTwo Controller*/
routes.MapRoute(
name: "Your route name 2",
url: "GameTwo",
defaults: new
{
controller = "GameTwo",
action = "Index"
}
);
/* default*/
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}}
Are you giving correct controller name?. I am just seeing your url as
mysite.com/gametwo
but controller name as GameTwo Pls change it as GameTwo and try again.

Getting forbidden (403.14) when not going to /index but just /

With ASP.NET MVC, I have code as below and the standard route definition. When I navigate to mysite.com/ExtjsRun I get a 403.14 error but when I go to mysite.com/ExtjsRun/index I get the controller executed.
My question is how to get the route /ExtjsRun to default to my index method.
using System.Web.Mvc;
using WebApp.Models;
namespace WebApp.Controllers
{
public class ExtJsRunController : Controller
{
[MultiTenantControllerAllow("svcc,angu")]
public ActionResult Index()
{
var str = "/ExtjsRun/" + Tenant.Name;
return Redirect(str);
}
}
}
Routing:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
namespaces: new[] {"WebApp.Controllers"},
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home",
action = "Index",
id = UrlParameter.Optional }
);
Change the Routing code as below
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
namespaces: new[] {"WebApp.Controllers"},
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "ExtJsRun",
action = "Index",
id = UrlParameter.Optional }
);
You are not set the default controller to ExtJsRun and action to Index. By your code, the iis server searches for the Home controller and Index Action in that controller, that's the reason for your access denial
Hope this helps

Url routing with static name mvc

I have a controller name Dashboard and inside that controller i have an action AdminDashboard . Now by default url of this action becomes /Dashboard/AdminDashboard . I want to map this action to this url /SupervisorDashboard
This is what i am doing but its saying not found
routes.MapRoute(
name: "SupervisorDashboard",
url: "SupervisorDashboard",
defaults: new { controller = "Dashboard", action = "AdminDashboard" }
);
and also how can i redirect to this page using Url.Action
Global.asax
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "SupervisorDashboard",
url: "SupervisorDashboard",
defaults: new { controller = "Dashboard", action = "AdminDashboard" }
);
}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
Have you placed this new route definition before default route? Routes are evaluated in the same order in which they were registered. If you put default route before any of custom routes, it will be used (and since you probably don't have any SupervisorDashboardController in code, 404 will be returned).
Url.Action should work correctly, if routes are defined in correct order.
So, for this case, RouteConfig should look like this:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// this one match /SupervisorDashboard only
routes.MapRoute(
name: "SupervisorDashboard",
url: "SupervisorDashboard",
defaults: new { controller = "Dashboard", action = "AdminDashboard" }
);
// should be last, after any custom route definition
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}

Resources