MVC custom routing setup - asp.net-mvc

I'm new to MVC. I've got my default routing setup as :
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "Demo.Controllers" }
);
That works beautifully.
I'd like to setup the routing to also accommodate the following path :
Apps/DemoApp1 ">>controllers>>models>>views under Demo1"
So that I'd be able to add a folder called DemoApp2 under Apps and still have it work the same. The folder Apps must act like an Area if that makes sense.

Related

Remove home directory from the URL with ASP.NET MVC

Situation
I'll build my URL like follow:
The overview page on / (at HomeController).
The detail page on /details/123 instead of /home/details/123 knowing that 123 always is a number (at HomeController).
An info page on /info instead of /home/info (at HomeController).
The logon page on /account/signin (at AccountController).
I've don't have more pages on my application.
Try 1
I've created two routes like follow:
routes.MapRoute(
name: "Home",
url: "{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 }
);
The problem is that the account page gives a 404. The other pages works.
Try 2
I've created one route like below:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
On each method inside the HomeController added a the Route attribute with the correct URL.
The problem is now that the overview and logon pages works the others gives now a 404.
Question
How could I remove the home directory from the URL with ASP.NET MVC?
To use attribute routes you need to add
routes.MapMvcAttributeRoutes();
to your RouteConfig.cs file.
It should go before your default route.

how can limit the action the action in specific namespace?

I have create a solution, and add the four projects into it.
that contains three mvc site( SiteA,SiteB,SiteC)
and a common core class library(name is core project).
now all of the mvc sites's Controllers was moved to the core project.
and I reference the core project to these mvc sites.
I try to register routes in these sites.
SiteA:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "AWebDefault",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new string[] { "Core.AWeb.Controllers" }
);
SiteB:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "BWebDefault",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new string[] { "Core.BWeb.Controllers" }
);
SiteC:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "CWebDefault",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new string[] { "Core.CWeb.Controllers" }
);
the question is :
when i visit the siteA, and how to prevent the user to vistit the siteB controller or SiteC Controller?
I want to the controller only can be found in the special namespace. can not search in other namespace. so how to solve it?
You can create action filter or custom action filter and use it before actions or controllers. for this store namespace name and check in filter. if namespace of target action is equal by namespace name then allow access. Another solution is to use Area. To generate a link to a different area, you must explicitly pass the target area name in the routeValues parameter for these methods (Linking Between Areas).

MVC routing and deployment

I have been struggling with this for a lot of days now. I am a novice to MVC and I m stuck with these issues. I have a mvc application. Following is my route.config file which I understand from a previous post is not proper. My pages are fairly straightforward, there are no query strings, except for one page. every page is controller/view format. What would be the correct routing config for such a configuration. If somebody can help me with that with a little explanation it ll be great. When I deploy that application in IIS, I m having trouble bcoz of this routing.
when I deploy in IIS and I do a browse it directly goes to my login page which is good but internally its going to
http://localhost/Home/accountdetails
instead of going to
http://localhost/MyWebsite/Home/accountdetails
due to which I m getting a 404 error.I don understand how to set this up. Also my images which are in content folder are not showing up either bcoz they are referring to
http://localhost/Content/Images/myimage
while they are actually under
http://localhost/MySite/Content/Images/myimage
how can I correct these issues. Please provide some assistance here.
Here is my route.config
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Home",
url: "{controller}/{action}/{id}",
defaults: new
{
controller = "Home",
action = "Login",
id =
UrlParameter.Optional
}
);
routes.MapRoute(
name: "Invoice",
url: "{controller}/{action}/{q}",
defaults: new
{
controller = "Home",
action = "GetInvoice",
id =
UrlParameter.Optional
}
);
routes.MapRoute(
name: "Error",
url: "{controller}/{action}/{id}",
defaults: new
{
controller = "Home",
action = "Error",
id =
UrlParameter.Optional
}
);
routes.MapRoute(
name: "ResetPassword",
url: "{controller}/{action}/{id}",
defaults: new
{
controller = "Home",
action = "ResetPassword",
id
= UrlParameter.Optional
}
);
routes.MapRoute(
name: "Accounts",
url: "{controller}/{action}/{id}",
defaults: new
{
controller = "Home",
action = "AccountStatus",
id
= UrlParameter.Optional
}
);
routes.MapRoute(
name: "Register",
url: "{controller}/{action}/{id}",
defaults: new
{
controller = "Home",
action = "Register",
id =
UrlParameter.Optional
}
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new
{
controller = "Home",
action = "Login",
id =
UrlParameter.Optional
}
);
}
}
Update
For images I am setting the background image as below
<div class="bannerOuter" style="background-image: url('../../Content/Images/AccountStatus_cropped-23.jpg');">
but after publishing when I run the application and I press f12 I see that its referring to the below url for the image and throws a 404 error.
http://localhost/Content/Images/Accounts_cropped-23.jpg
But I can browse to it if I change the url to add my website name that I specified in IIS.
http://localhost/MyWebsite/Content/Images/Accounts_cropped-23.jpg
So this is the problem what should be my image path in this case.
Edit
#Html.EncodedActionLink(string.Format("{0} Statement", #Convert.ToDateTime(#invoiceItem.InvoiceDate).ToString("MMMM")), "GetInvoice", "Home", new { accountNumber = Model.CustomerModel.AccountNumber, dueDate = (Convert.ToDateTime(invoiceItem.DueDate).ToString("dd-MMM-yyyy")) }, new { target = "_blank", #class = "Link" })
So the url here is coming as
http://localhost/Home/GetInvoice?q=ZDmgxmJVjTEfvmkpyl8GGWP4XHfvR%2fyCFJlJM1s0vKj4J6lYJWyEA4QHBMb7kUJq
how can I change it to be
http://localhost/MySite/Home/GetInvoice?q=ZDmgxmJVjTEfvmkpyl8GGWP4XHfvR%2fyCFJlJM1s0vKj4J6lYJWyEA4QHBMb7kUJq
Relative paths in inline CSS are problematic because it will be relative to the current URL. So if you go to http://localhost/MySite/Home/Login you will need to use ../../Content/myimage.png. But, if you go to http://localhost/MySite, the default controller and action (Home/Login) from route.config will kick in, and now the relative path to the image is Content/myimage.png because the controller and action are not explicitly in the URL. The same problem will happen if you go to http://localhost/MySite/Home; the default action (Login) will be used and the relative path is ../Content/myimage.png.
The best solution is to use styles in CSS files and not use inline styles; that way, the image URLs are always relative to the CSS file, whose URL never varies. Just be aware if you use bundling in BundleConfig.cs you need to make sure the path stays consistent MVC Bundling and CSS relative URLs
But if you need to use inline styles, you can use #Url.Content() to always get the correct path, with ~ being the root of your app:
style="background-image: url('#Url.Content("~/Content/myimage.png")'"

Mixing WebForms and MVC5 routing not working

I have below code for my routing for web forms and MVC.
MVC routing seems to be working fine but not the web form when I mix both.
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("");
//MVC
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
//WebForms
routes.MapPageRoute(
"myPage",
"page/companyInfo/{*queryvalues}",
"~/company/details.aspx"
);
Do I need to write a IgnoreRoute statement for details.aspx page?
Change the order of the routes, the MVC route should be at the bottom as default is basically a catch-all. MVC process routes from top to bottom, if it finds a match, it stops looking and routes you to the matched route.
//WebForms
routes.MapPageRoute(
"myPage",
"page/companyInfo/{*queryvalues}",
"~/company/details.aspx"
);
//MVC
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

ASP.NET MVC 4 Core routing

I have set up the following structure for my website. (see link below)
http://postimg.org/image/ut4klihrp/
localhost:62540 Goes to the index page of the core
localhost:62540/www/Home Goes to the index page of the WWW
localhost:62540/cms/Home Goes to the indec page of the cms
I basically want the 'default' route (localhost:62540) to go to my WWW project. How can i do this or does anybody know a tutorial were the principle for this get explained? Or is this not possible since i use the area method.
Eventually i want to remove the view and controller from the core project.
Route config in www :
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new string[] { "WWW.Controllers" }
);
You can specify the default area in the defaults:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { area = "www", controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new string[] { "WWW.Controllers" }
);

Resources