controller is not picking up the route - asp.net-mvc

My route looks like:
routes.Add(new Route("{companyName}/{action}/{id}", new MvcRouteHandler())
{
Defaults = new RouteValueDictionary(new { controller = "CompanyController", action = "Index", id = 1 }),
}
);
my action:
public ActionResult Index(string companyName, string id)
{
Response.Write(companyName);
Response.End();
return ViePage("~/views/company/index.aspx");
}

try this:
routes.Add(new Route("{companyName}/{action}/{id}", new MvcRouteHandler())
{
Defaults = new RouteValueDictionary(new { controller = "Company", action = "Index", id = 1 }),
}
);
when referencing your controllers you don't want to have the "controller" part of the name there.

Related

ASP.NET MVC Overriding Index action with optional parameter

I want to accept /User/ and /User/213123
Where 213123 is a parameter (user_id)
Here is my RouteConfig.cs:
routes.MapRoute(
name: "user",
url: "{controller}/{action}/{username}",
defaults: new { controller = "User", action = "Index", username = UrlParameter.Optional }
);
And my UserController.cs:
public ActionResult Index()
{
ViewData["Message"] = "user index";
return View();
}
[Route("user/{username}")]
public ActionResult Index(string username)
{
ViewData["Message"] = "!" + username + "!";
return View();
}
This works in .net-core 1.0 but not in mvc5. What am I missing?
Thank you
EDIT:
Having just this in my UserController.cs also doesn't work (returns 404):
public ActionResult Index(string username)
{
if (!String.IsNullOrEmpty(username))
{
ViewData["Message"] = "Hello " + username;
}
else
{
ViewData["Message"] = "user index";
}
return View();
}
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Requested URL: /user/asd
EDIT2:
Updated RouteConfig.cs:
routes.MapRoute(
"userParam", "user/{username}",
new { controller = "user", action = "IndexByUsername" },
new { username = #"\w+" }
);
routes.MapRoute(
name: "user",
url: "user",
defaults: new { controller = "User", action = "Index"}
);
/User/ now calls IndexByUsername with Index as the username
/User/asd still returns 404
EDIT4: Current code:
RouteConfig.cs:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/",
defaults: new { controller = "Home", action = "Index" }
);
routes.MapRoute("userParam", "user/{username}", new { controller = "user", action = "Index"});
routes.MapRoute("user", "{controller}/{action}/{username}", new { controller = "User", action = "Index", username = UrlParameter.Optional });
UserController.cs:
public class UserController : Controller
{
public ActionResult Index(string username)
{
if (!String.IsNullOrEmpty(username))
{
ViewData["Message"] = "Hello " + username;
}
else
{
ViewData["Message"] = "user index";
}
return View();
}
}
You need only one action method with the signature
public ActionResult Index(string username)
and in that method you can check if the value of username is null or not.
Then you route definitiosn needs to be (note the user route needs to be placed before the default route)
routes.MapRoute(
name: "user",
url: "user/{username}",
defaults: new { controller = "User", action = "Index", username = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

Url Routing Help Asp.net Mvc

I have three url types. These:
first: http://localhost/
second: http://localhost/X/
third: http://localhost/X/Y/
Examples Url:
http://localhost/test/
http://localhost/test/details/
first:
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new
{
controller = "Home",
action = "Index",
id = UrlParameter.Optional
}
);
public class HomeController : Controller
{
// GET: /Home/
public ActionResult Index()
{
return View();
}
}
second:
routes.MapRoute(
"Module",
"{module_name}/{controller}/{action}",
new
{
controller = "Module",
action = "Index",
module_name = UrlParameter.Optional
}
);
public class ModuleController : Controller
{
//
// GET: /Module/
public ActionResult Index(string modul_name)
{
return View();
}
}
third:
routes.MapRoute(
"ModuleDetails",
"{module_name}/{details_param}/{controller}/{action}",
new
{
controller = "ModuleDetails",
action = "Index",
module_name = UrlParameter.Optional,
details_param = UrlParameter.Optional
}
);
public class ModuleDetailsController : Controller
{
//
// GET: /ModuleDetails/
public ActionResult Index(string modul_name, string details_param)
{
return View();
}
}
in this instance;
http://localhost/X/
response: "Home", "Index"
but;
http://localhost/X/
response: Application in the server error. Resource Not Found.
http://localhost/X/Y/
response: Application in the server error. Resource Not Found.
How can I do?
Thanks, best regards..
http://localhost/X/
response: Application in the server error. Resource Not Found.
This happens because each of your routes specifies at least 2 mandatory parameters.
Try to add this one:
routes.MapRoute(
"Default",
"{controller}/{action}",
new
{
controller = "Home",
action = "Index"
}
);
that's right friends..
routes.MapRoute(
"Default", // Route name
"", // URL with parameters
new { controller = "Home", action = "Index" } // Parameter defaults
);
routes.MapRoute(
"Module",
"{modul_name}",
new { controller = "Modul", action = "Index", modul_name = UrlParameter.Optional }
);
routes.MapRoute(
"Page",
"{modul_name}/{page_name}",
new { controller = "Page", action = "Index", modul_name = UrlParameter.Optional, page_name = UrlParameter.Optional }
);

Custom Route Not Working in mvc 4

I have created one custom route
routes.MapRoute("Exams",
"Exams/{id}/{name}",
new { controller = "Exam", action = "SingleExam", id = "", name = (string) null },
new[] { "MockTests.FrontEnd.Controllers" }
);
For this I am creating link as
http://localhost:61575/Exams/12/maharashtra+mba+common+entrance+test
But his gives error as
The resource cannot be found
SingleExam code
public ActionResult SingleExam(int id,string name)
{
return View();
}
Other Routes
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "User", action = "Register", id = UrlParameter.Optional },
new[] { "MockTests.FrontEnd.Controllers" }
);
What wrong I am doing ?

ASP .NET MVC What's going on with my route?

I have a problem, my url is ugly smth like this:
http://localhost:43547/Admin?id=1&str=wooh
I want see the url like this one
http://localhost:43547/LOL
My routes are:
routes.MapRoute(
"MyRoute",
"LOL",
new { controller = "Admin", action = "Index",
id = UrlParameter.Optional, str = UrlParameter.Optional }
);
routes.MapRoute(
"Default",
"{controller}/{action}",
new { controller = "Home", action = "Index" }
);
and I have 2 identical views one of them is with such method
#Html.ActionLink("Click me", "Index", "Admin", new { id = 1, str = "wooh" }, null)
which moves the page...
and I have 2 controllers:
public ActionResult Index(int id = 5485, string str = "Default Value")
{
ViewBag.ID = id;
ViewBag.STR = str;
return View();
}
public class HomeController : Controller
{
public ActionResult Index()
{
ViewBag.Controller = "Home";
ViewBag.Action = "Index";
return View();
}
}
So what's wrong? When I type smth like /LOL
It moves me to another page with default values of id and str...
Change your MyRoute definition to include the id and str parameter values.
routes.MapRoute(
"MyRoute",
"LOL",
new { controller = "Admin", action = "Index",
id = 1, str = "wooh" }
);
That way your url will look like /LOL but will pass the values you need.

MVC : Separate admin controllers

I was wandering if there is option to do the following
If I call
"admin/Category" - to call "CategoryAdminController"
If I call
"Category" - to call "CategoryController"
It is very easy to do this via routing and custom controller factory. Here is the solution:
// add route
routes.Add(new Route("{culture}/admin/{controller}/{action}/{*id}", new MvcRouteHandler())
{
Defaults = new RouteValueDictionary(new { controller = "Home", action = "Index", id = "", culture = LocalizationManager.DefaultCulture.Name, controllerSufix = "Admin" }),
Constraints = new RouteValueDictionary(new { culture = new CultureRouteConstraint() })
});
Than create custom controller factory
public class CmsControllerFactory : DefaultControllerFactory
{
RequestContext _requestContext;
protected override Type GetControllerType(string controllerName)
{
if (_requestContext.RouteData.Values.ContainsKey("controllerSufix"))
{
string sufix = (string)_requestContext.RouteData.Values["controllerSufix"];
Type type = base.GetControllerType(String.Concat(controllerName, sufix));
if (type != null)
return type;
}
return base.GetControllerType(controllerName);
}
public override IController CreateController(RequestContext requestContext, string controllerName)
{
_requestContext = requestContext;
return base.CreateController(requestContext, controllerName);
}
}
I would like if anybody know some different/better solution.
You can do this quite simply with two route handlers:
routes.MapRoute(
"Admin",
"/admin/category/{id}",
new { controller = "CategoryAdminController", action = "Index", id = "" }
);
and then:
routes.MapRoute(
"Standard",
"/category/{id}",
new { controller = "CategoryController", action = "Index", id = "" }
);

Resources