from here i got a little touch about Setting an alternate controller folder location in ASP.NET MVC. here is the url Setting an alternate controller folder location in ASP.NET MVC
they guide us we can change the namespace and also specify the name space in routine code and these way we can solve it but this above link is not showing how to change or store controller related .cs files in other folder location.
suppose i want to store controller in folder called mycontroller in root then what i need to do. guide me please. thanks
UPDATE
You can do this using Routing, and keeping the controllers in separate namespaces. MapRoute lets you specify which namespace corresponds to a route.
Example
Given this controllers
namespace CustomControllerFactory.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return new ContentResult("Controllers");
}
}
}
namespace CustomControllerFactory.ServiceControllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return new ContentResult("ServiceControllers");
}
}
}
And the following routing
routes.MapRoute(
"Services",
"Services/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new string[] { "CustomControllerFactory.ServiceControllers" } // Namespace
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new string[] { "CustomControllerFactory.Controllers"} // Namespace
);
You should expect the following responses
/Services/Home
ServiceController
/Home
Controllers
There is no need to do anything, storing controllers in Controllers is only convention, not a requirement. Basically you can store controllers in any folder.
Check your RouteConfig.cs and you will probably find that a namespace has been defined for each route. You controller classes can be placed anywhere in the project as long as they still match the namespace defined in your routes.
Related
I am getting the following error when debugging my header links.
I have tried recreating my Index.cshtml (Empty and with Razor/HTML) and my ServicesController.
If I browse to Services/Index explicitly then the page loads up correctly.
IE: localhost:58069/Services/ does not work but localhost:58069/Services/Index does
My other controllers are working correctly using the same structure and ActionLink helper.
Not working code:
public class ServicesController : Controller
{
// GET: Services
public ActionResult Index()
{
return View();
}
}
Action Link HTML Helper for Services/Index
<li>#Html.ActionLink("Services", "Index", "Services")</li>
Working Code
public class HomeController : Controller
{
// GET: Home/Index
public ActionResult Index()
{
return View();
}
}
Working Helper
<li>#Html.ActionLink("Welcome", "Index", "Home")</li>
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 }
);
}
Tried the following SO solutions:
ASP MVC in IIS 7 results in: HTTP Error 403.14 - Forbidden
This behavior is caused by the Services folder (below Scripts folder) you have in your project. When hosting in IIS, you should not have a file or folder with the same name as your ASP.NET route.
Ways to solve this:
routes.RouteExistingFiles = true; will let ASP.NET handle all the routing
Rename your Services folder or controller
Move Services to a separate library and remove the folder from the web project
I'd recommend the latter option as settings RouteExistingFiles to true would need additional research to double-check you're not opening any security holes and it cleans up your web project (a little bit of separation never hurts).
I have two controllers with same name in different namespaces
-Controllers
--AdminCp
---AccountController.cs
--Cp
---AccountrController.cs
And same view location structure
-Views
--AdminCp
---Account
----Login.cshtml
--Cp
---Account
----Login.cshtml
My Account controllers contains method Login
public ActionResult Login()
{
return View();
}
I also have custom RazorViewEngine
public ExtendedRazorViewEngine()
{
ViewLocationFormats = new[]
{
"~/Views/AdminCp/{1}/{0}.cshtml",
"~/Views/Cp/{1}/{0}.cshtml",
"~/Views/{1}/{0}.cshtml",
"~/Views/Layout/{0}.cshtml",
};
}
My routing uses MvcCodeRouting
routes.MapCodeRoutes(typeof (Controllers.HomeController));
But when i trying route to the action Login in Cp namespace i am getting view from AdminCp namesapce
http://example.com/cp/account/login ---> /views/admincp/account/login.cshtml
I know that i can pass path locatin in View() But... It looks ugly.
Can somebody help me with this? Thanks in advance!
You need to have Areas to implement such function.A good link to start is :
Areas
Also,you need to change your RouteConfig:
context.MapRoute(
"Default",
"{area}/{controller}/{action}/{id}",
new { area = "MyOneWay", controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Furtermore,ypou can have your controller in below defined way:
namespace MyApp.Areas.MyOneWay.Controllers
{
public class HomeController : Controller
{
...
}
}
namespace MyApp.Areas.MySecondWay.Controllers
{
public class HomeController : Controller
{
...
}
}
In my application I have an area "Member".
Outside this member area I have a folder named "Generic" which is having a controller "DataBindController".
This controller will be used in all areas. So to keep it common, i am keeping it in a separate folder outside of areas.
My route config is as follows:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
var ObjRoute = routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults:new {controller = "Login", action = "MemberLogin", id = UrlParameter.Optional},
namespaces: new string[] { "MyApp.Generic.*" }).
DataTokens = new RouteValueDictionary(new { area = "Member"});
//ObjRoute.DataTokens["UseNamespaceFallback"] = false;
}
}
Here's the project directory structure.
The Test controller inside Generic folder is as follows:
namespace MyApp.Generic
{
public class DataBindController : Controller
{
public ActionResult Test()
{
return Content("Test");
}
}
}
I am getting following error when I call the test controller using "http://localhost/MyApp/Generic/DataBind/Test"
Error in Path :/MyApp/Generic/DataBind/Test The controller for path
'/MyApp/Generic/DataBind/Test' was not found or does not implement
IController.
Please give me some idea on this issue.
your address doesn't match your defined route. Generic is just a folder. MVC doesn't care about the folder your controller is in.
the correct one should be like this :
http://localhost/MyApp/DataBind/Test
so the DataBind will be the controller and Test the Action.
Update:
your route is
{controller}/{action}/{id}
When you have Generic in your address, Asp.net matchs parts this way : Generic is the Controller, DataBind is Action and Test is the Id. of course it can't find such a thing. But when you remove Generic , every part goes to its real place.
To have Generic in the address, you should change your route to this:
Generic/{controller}/{action}/{id}
To read more about routing :
http://www.asp.net/mvc/overview/older-versions-1/controllers-and-routing/asp-net-mvc-routing-overview-cs
I was referencing this question to try and get this done, but it only works for my index method and I am not sure why.
My project has one area in it (if that is relevent) and I have about 5 different views that I want to hide /home/ in the url.
Code:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"JobResults", // Route name
"JobSearch/{title}-{id}", // URL with parameters
new { controller = "JobSearch", action = "Job" }, // Parameter defaults
new[] { "inkScroll.Web.Controllers" }
);
routes.MapRoute("home", "{action}",
new { controller = "Home", action = "index" });
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new
{
controller = "^(account|common|base|jobsearch)$", //every controller goes in here
action = "Index",
id = UrlParameter.Optional
},
new[] { "inkScroll.Web.Controllers" }
);
I am solving the same problem with the help of Attribute based Routing feature of ASP.NET MVC 5. Say, I have an action named ContactUs in my Home Controller as follows:
public ActionResult ContactUs()
{
return View();
}
I used to get Url for ContactUs as /Home/ContactUs. But, I wanted it to be simply /ContactUs. So, I am decorting the ContactUs action with Route Attribute as follows:
[Route("ContactUs")]
public ActionResult ContactUs()
{
}
So, this is working perfectly for me. Therefore, if you are using ASP.NET MVC 5, I would say, utilize this excellent feature of MVC5. It will not only save you from separating Route Logic and Actions, but also, it solves many problems very easily.
If ASP.NET MVC5 is not an option for you, or if you dont want to use Route Attribute on Action Methods, then, perhaps the following route can work: ( I did not test it though)
routes.MapRoute("Default", "",
new { controller = "Home", action = "index" });
This page contains helpful resource about Attribute Routing: http://blogs.msdn.com/b/webdev/archive/2013/10/17/attribute-routing-in-asp-net-mvc-5.aspx
Catch all wildcard route as the last one would work
routes.MapRoute("home2", "{*path}",
new { controller = "Home", action = "index" });
I am working on an ASP.NET MVC application. For some reason, everytime I think I understand routing, something pops up that I don't understand. Currently, I have two routes that I can't seem to figure out. My directory structure looks like the following
- Views
- Internal
- Profile
- Index.cshtml
- Input
- Page1.cshtml
In my global.asax.cs file, I have added the following mappings:
routes.MapRoute(
"UserProfileInfo",
"{controller}/profile",
new { controller = "Internal", action = "UserProfileInfo" }
);
routes.MapRoute(
"Page1",
"{controller}/input/page1",
new { controller = "Internal", action = "Page1" }
);
In MyController, I have the following:
public ActionResult UserProfileInfo()
{
return View("~/Views/internal/profile/Index.cshtml");
}
public ActionResult Page1()
{
return View("~/Views/internal/input/Page1.cshtml");
}
I want to store my actions in a single controller. I thought I had everything setup properly. But I continue to get a 404. What am I doing wrong?
Remove the "Controller" suffix from the controller name in your calls to MapRoute to create a mapping to a class called InternalController. The Controller suffix is appended by the framework when looking for a matching implementation. e.g.:
routes.MapRoute(
"UserProfileInfo",
"{controller}/profile",
new { controller = "Internal", action = "UserProfileInfo" }
);