MVC routing and deployment - asp.net-mvc

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")'"

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.

Set index Home page instead of Azures This mobile app is up and running

all my redirections in Route.Config is working fine, instead of my home default link is displaying Azure's This mobile app is up and running
http://localhost:51540/
Bellow is my RouteConfig code
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("");
routes.MapRoute(
name: "/Index",
url: "Index/{id}",
defaults: new { controller = "MyController", action = "Index", id = UrlParameter.Optional }
);
When I add my code without /index i get 404 on > http://localhost:51540/ and on > http://localhost:51540/Index.
routes.MapRoute(
name: "without /index",
url: "{controller}/{action}/{id}",
defaults: new { controller = "MyController", action = "Index", id = UrlParameter.Optional }
);
Does anybody know where can I change this configurations/settings?
Regards!
There is the .UseDefaultConfiguration() method invoked in the Startup.MobileApp.cs. The part of that is the AddMobileAppHomeController() which depends on the corresponding Microsoft.Azure.Mobile.Server.Controllers.HomeController reference. I believe it is what blocks you from the implementation of what you are doing.
Helpful post and announcement about that

ASP.Net MVC removing folder name in URL

Good morning/Afternoon
I have a view folder with another folder and then the view name
~Views/Edit/Edit.cshtml
I would like to keep the structure like that if possible, but the problem lies within the URL
It is currently showing
http://localhost:63672/Edit/Edit/4
I would like it to show
http://localhost:63672/Edit/4
Without the ID in the URL as well if possible, but that is a separate problem.
How do I achieve this?
My routeconfig currently shows
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Login", action = "Login", id = UrlParameter.Optional
}
Thanks
We can specify a route for Edit. Add this above the "Default" route currently in your routeConfig
Example
routes.MapRoute(
"Edit", // Route name
"Edit/{id}", // URL with parameters
new { controller = "Edit", action = "Edit", id = UrlParameter.Optional });

ASP.NET MVC routing to action

I'm having trouble doing what I want to achieve when doing routing in ASP.NET MVC.
What I want to do is the following:
When typing http://localhost/MyWebsite/, I want to redirect to http://localhost/MyWebsite/Login/ (The redirection to the action Index is implied here)
When typing http://localhost/MyWebsite/MyAction, I want to redirect to http://localhost/MyWebsite/Login/MyAction
Point 1 was achieved using the following lines in the file RouteConfig.cs:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Login", action = "Index", id = UrlParameter.Optional });
But I cannot accomplish point 2. Note that the user does not come from another action in a controller, but actually types the address in his browser.
Thanks for your help.
since /MyWebsite/Login and /MyWebsite/MyAction both have two segments in the URL, they're both matching your defined Route.
You can use a Route Constraint to only match /MyWebsite/Login to your first point, followed by a modified second route mapping:
routes.MapRoute(
name: "Default",
url: "MyWebsite/Login/",
defaults: new { controller = "Login", action = "Index", id = UrlParameter.Optional });
routes.MapRoute(
name: "Actions",
url: "MyWebsite/{action}/",
defaults: new { controller = "Login", 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