Admin App with the landing page, routing - c#

So far when I open root location, it is an admin home page (i.e. www.example.com), and i would like to make an landing page in that root location, but all admin things i would like to have in separate folder (i.e. www.example.com/admin/).
In example routing option for 'uses' currently is www.example.com/uses/ and I would like to have it in www.exaple.com/admin/users, but I dont want to change the project structure only routing. How can i make it with the minimal change?
EDIT:
I have tried registering routes in MvcApplication class in Global.asax.cs, but it didn't work
public static void RegisterRoutes(RouteCollection routes){
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default",
"admin/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = "" }
);
}
But this didn't work out.

For Asp, I would reference the following exert from Microsoft:
Routes
A route is a URL pattern that is mapped to a handler. The handler can be a physical file, such as an .aspx file in a Web Forms application. A handler can also be a class that processes the request, such as a controller in an MVC application. To define a route, you create an instance of the Route class by specifying the URL pattern, the handler, and optionally a name for the route.
You add the route to the application by adding the Route object to the static Routes property of the RouteTable class. The Routes property is a RouteCollection object that stores all the routes for the application.
You typically do not have to write code to add routes in an MVC application. Visual Studio project templates for MVC include preconfigured URL routes. These are defined in the MvcApplication class, which is defined in the Global.asax file.
Src. https://msdn.microsoft.com/en-us/library/cc668201.aspx?f=255&MSPPError=-2147217396
What I would do is create an ADMIN controller, and then set up routes something like so: {controller}/{page}/{action}
Alternatively, you can follow this solution to rewrite routes to subfolder: https://stackoverflow.com/a/17074898/2267583

Related

Redirect routing in ASP.NET Core 2.1

I have a main project with some controllers, e.g. HomeController containing an action "Index". This action is reached by www.mysite.com/home/index.
Then I have another project called "plugin", which is referenced within the main project.
There is a controller, e.g. CustomerController. An action within this controller contains a routing attribute "[Route("edit")]".
This action is reached by www.mysite.com/customer/edit. But I want that this action can be reached by www.mysite.com/plugin/customer/edit containing the name of the project (or another name).
How can I do this without setting routing attribute for every controller in my "plugin" project?
Btw.. I'm using NopCommerce 4.1 if it's necessary to know..
This is scenario for Areas.
1) Inside your plugin create folder structure
Areas
..Plugin
....Controllers
....Views
2) Inside controllers create base plugin controller "PluginController" where you set Area attribute
[Area("Plugin")]
public class PluginController : Controller
{
...
}
3) Make all your plugin controllers inherit from PluginController
public class CustomerController : PluginController
{
...
}
4) Add support for areas into route builder
app.UseMvc(routes =>
{
routes.MapRoute(
name: "defaultWithArea",
template: "{area:exists}/{controller=Home}/{action=Index}/{id?}");
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
Now all actions inside your plugin will require www.mysite.com/plugin/ ...
I'll also note that If you wish to retrieve action urls from outside the plugin you need to specify area of the controller like so:
#Url.Action("Edit", "Customer", new { Area = "Plugin" })

MVC manipulate URL (routing), is it possible?

I have a website that use this pattern.
http://www.domain.com/product/...
My question is now, i need to create a subsite that going to be with this URL pattern, i have tried to change the routing without success.
http://www.domain.com/companyname/product/...
How can i inject the companyname in the URL without breaking my current routing?
Thanks
Niden
Three ways:
If it's relatively static, you can follow Andy's advice in the comments and publish the site in a virtual directory, companyname. Assuming you've properly used the UrlHelper extensions to generate URLs, instead of just hard-coding paths, then everything will just work.
You can create a "companyname" area. The default routing for an area is /area/controller/action. So that would get you the URL structure you want. However, areas are somewhat segregated, so you would need to copy controllers and views to the area's directory. Although, you could subclass controllers from the main app in the area to reuse code.
Just change the default route/add a new route:
routes.MapRoute(
"CompanyDefault",
"{company}/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = "" }
);
// default route here

Calling controllers in different folders in ASP.NET WebAPI

I have an 100% javascript application inside my ASP.NET WebAPI project. This application consumes all the data from my API methods.
Now, I have been requested to create a simple portal built in Razor and HTML also inside my WebAPI project (so like a MVC project).
So I have created a folder under "Controllers" folder named "Portal" where Im placing all my portal exclusive controllers.
Here is the Controllers structure folders:
This is my Razor web application views structure:
So when I run the application I can see the Login page view (under "Account" folder). When I click my Login button, I call my CommonController (Portal) to render a Left menu but I get the error:
I guess because I have 2 CommonControllers, but 1 is for serving my javascript application and the other is for my Portal application.
The line in my Index.cshtml that calls the Common controllers is:
<div class="page-header page-header-blue">
#Html.Action("Header", "Common")
<h1><i class="icon-bar-chart"></i> Dashboard</h1>
</div>
In my WebApiConfig.cs I have the following Web Api Route:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}"
);
So I don't know how to tell it that I want to call CommonController that is under "Portal" folder.
Any clue or advice on how to do this and the best practice?
UPDATE:
I have create other route for my Portal controllers:
config.Routes.MapHttpRoute(
name: "DefaultMvc",
routeTemplate: "portal/{controller}/{action}"
);
Seems to work but in my index.cshtml calls : #Html.Action("Header", "Common") how can I tell the action to call Portal Common and not just Common ?
I think you can use areas for your case.
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Billing_default",
"Admin/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
And you need update Application_Start method
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
Area in MVC 5 with example - step by step

How to ignore all characters after "controller/action" in an ASP.NET MVC route?

I would like my ASP.NET MVC4 application to only serve the base HTML markup for a specific page, and after that I'm processing everything else on client-side with knockout.js/history.js/AJAX, including the initial page load.
So when someone refers to URL http://example.com/products/list/food/fruits, the MVC router should simply ignore everything what is behind "products/list" and route the request to ProductsController and List action. Then on client-side I will handle the rest and load the requested data accordingly.
I was playing with the route definitions, I tried to completely skip the "products/list" route, I also tried to add a "products/list/*" route, but didn't have success yet.
You can use an asterisk as part of the last variable in a route. For example, when configuring your routes:
routes.MapRoute(
"ProductRoute",
"products/list/{*otherArgs}",
new { controller = "Products", action = "List" });
You can learn more in MSDN's Documentation on routing under the section "Handling a Variable Number of Segments in a URL Pattern"
You will need to create your own route.
Something like this should do the trick:
routes.MapRoute("Products", "Products/{List}",
new {controller = "Products", action = "List"}
);
Note: I´m not sure if the other parameters are required in the route.

Implicit connection of Action Methods with Views in ASP.NET MVC

I am currently working in a brownfield ASP.NET MVC 3 project in VS2010.
In this project, views and controllers are in separate projects. This is not something that I have seen before. In each action method there is no explicit stating of view name as below.
return View("viewName",passingModel);//projects where controllers and views are in same
I have done this implicitly in VS2012 by right clicking on the view and do add view. So I was not bothered about where is this connection between action method's return view and the view is stated.
Unlike in VS2012, in VS2010 I can not navigate to the view that is related to one particular action method by right clicking on View and doing go to view.
I tried to understand this by doing this small experiment. I created a Controller and created a Action Method call xxxx and I created a view for that implicitly as mentioned above and searched the word xxxx in entire solution but this word only appeared in controller and in the view.
So, I was unsuccessful in finding the answer. I think visual studio itself creating its own mapping to achieve this.
I would like to know who these implicit connections are created among action methods and views to understand what is going on in my project.
Edit:
Both the projects which contains controllers and views are class libraries. not asp.net mvc projects.
Global.aspx file contains this:
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}
protected void Application_Start()
{
DependenciesHelper.Register(new HttpContextWrapper(Context));
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RoutingHelper.RegisterRoutes(RouteTable.Routes);
}
protected void Application_End()
{
//Should close the index
//If this method is not executed, the search engine will still work.
SearchService.CloseIndex();
}
The mapping is fairly straightforward. For example if you have a controller called "MyBrilliantController" and an action method called "MyExcellentAction" which returned just return View(); it would map to (in the UI project) ~/Views/MyBrilliant/MyExcellentAction.cshtml
The only time where this is different is when you are working with "Areas" - but the mapping is effectively the same, it would just consider the area folder first (ie ~/Areas/MyArea/Views/MyBrilliant/MyExcellentAction.cshtml)
Hope that helps.
EDIT - You can also specify namespaces in the global.asax file on each route for the engine to find controllers
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new {
controller = "Home",
action = "Index",
id = UrlParameter.Optional
}, // Parameter defaults
new string[] {
// namespaces in which to find controllers for this route
"MySolution.MyControllersLib1.Helpers",
"MySolution.MyControllersLib2.Helpers",
"MySolution.MyControllersLib3.Helpers"
}
);
}

Categories