MVC Deployment Problem - Site Loads but Links (Routes) do not - model-view-controller

Having a problem deploying an MVC application.
Basically the site loads correctly, the home page appears. However anything which needs to access a controller action does not. So all the links just throw up 404 errors.
Does anyone have an Idea why the site loads but after that the controller actions appear not to?
Thanks

Are you running your app in IIS 6? If so you'll need to configure the .mvc extension or configure wildcard mapping. Steve Sanderson has a good post on it.

The fact that the home page appears indicates that you have at least one controller working properly. Namely, the HomeController.
You should check that you are following the default conventions (if you have it set up that way)
Controllers belong in the Controllers folder and follow the naming convention [Name]Controller.
Also, every action in the controller must be public and must return an ActionResult of some kind. Returning a View will cause a particular View to be rendered.
Also, Views follow the folder structure View/[ControllerName]/[Action].aspx
The fact that the first page loads means you probably have Home/Index set up properly for both your Controller and your View. You should take a look at those and see what the difference between that is and the other controllers/actions/views that you've set up.

Related

Redirecting issues with spring and springmobile

I'm building a website using Spring and the SpringMobile extension.
I've configured the LiteDeviceDelegatingViewResolver with mobilePrefix = m/, tabletPrefix = t/ and enableFallback = true, so that, whenever a dedicated mobile (or tablet) view isn't available, the "normal" (desktop site) view is served.
The site has several sections and subsections. For instance, there's a Products section and several Categories within it.
Currently, there isn't a landing page for Products, so I redirect the user to the first Category. In other words, when the link to /products is clicked, the corresponding #Controller method redirects to /products/category1 (by returning "redirect:/products/category1"); then, the method mapped to /products/category1, serves the view (by returning "viewname").
This works as expected when using the desktop site. However, in the mobile site, whenever I try to browse to /products, I get automatically redirected to /m/products by SpringMobile (which is fine), but then my #Controller gets into action, and, instead of being eventually redirected to /m/products/category1, strangely I get redirected to /m/products/m//products/category1.
Any ideas on why is this happening, and on how to achieve the expected behavior?
P.S.: If I navigate directly to /m/products/category1, the desktop view (the fallback) is properly shown.
Well, it turns out this was a bug in SpringMobile(MOBILE-70, MOBILE-78) which was fixed in version 1.1.0.RC1. It also affected forward: redirection.
I've updated my project dependencies, and the issues appear to be gone for good.

Adding a view to MVC that doesn't belong to a controller

I'm not sure if the question title really explains what I want to do, however I will explain below:
I am using the Visual Studio MVC project template and I have changed some of the tabs to map to different actions from different controllers. However I want to make one of the tabs to open a view that will again have links for different administration actions.
The problem I have is that I am unsure where to place this view as it doesn't really belong to an admin controller as each tab on this view will link to a list view in another controller. In effect it is a sub _Layout view, as it doesn't have anything to do with a controller.
I hope I have made myself clear enough!
You can place this view in the Shared folder since it will be used by multiple controllers. Or, you could place it somewhere else and reference it by using the full path to this View/Partial View
In a Controller
public ActionResult SomeAction(){
return View("~/Path/To/View/ViewName.cshtml");
}
In a View (Razor)
#Html.RenderPartial("~/Path/To/View/ViewName.cshtml");
With that said, the Shared folder makes the most sense since it will be shared across multiple controllers.

Return View From Controller in Area Not Working

After reading several posts on this subject I have yet to find an answer to my problem. I have an MVC 3 application and have added an Area to it. Everything works great until I try an return a view from a controller within the Area.
I can successfully post to the controllers Save Method but upon simply returning the view (return View()) I get the following:
The view 'Save' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Areas/Test/Views/Default1/Save.aspx
~/Areas/Test/Views/Default1/Save.ascx
~/Areas/Test/Views/Shared/Save.aspx
~/Areas/Test/Views/Shared/Save.ascx
...
This seems so basic, not sure why I am running into so much trouble.
Ive used Phil Haack's RouteDebugger (http://nuget.org/packages/routedebugger) and all routes are working as setup...
MVC is expecting (by the convention) a view (with the same name as your action name in any one of the folders (by default, But you can override this). You should have the view in any of the folders. That is the MVC convention. So add your view to that folder. You can add it by right clicking the Return View() statmenet in your action method and selecting Add View option. It will automatically add one view.
Or you can right click on the Areas/Test/Views/Default1 folder and select Add View and save it with the same name as of your Action method. If you want to save it with a differnt name than the action name, you can use the View method like this
return View("MyOtherViewName");
Assuming that you added a MyOtherViewName.cshtml as your View in the Areas/Test/Views/Default1 folder

MVC3 and IIS6 RedirectToAction redirects to wrong URL

This is weird. I have a virtual directory setup for an MVC3 application called (for the sake of this question) I'll call 'foobar'. The full URL to this site is:
http://localservername.domainname.com/foobar
In my logon form, I have the following line that is supposed to redirect to the main/home page of the application after logon.
return RedirectToAction("Index", "Home");
However, when this line executes, I get redirected to the wrong location - so instead of redirecting me back to:
http://localservername.domainname.com/foobar
I get redirected back to:
http://localservername.domainname.com/foobar/foobar
Which of course gets me a resource not found error. Also, any links in my views seem to do the opposite - for example if I have a link like this:
Widget Search
I would expect the following URL to open:
http://localservername.domainname.com/foobar/WidgetSearch
Instead, I get redirected to
http://localservername.domainname.com/WidgetSearch
Which of course also gets me a resource not found error. I've never encountered this type of behaviour before. I've gone over the basic and advanced settings and created a new application pool. Fiddler also shows me that (of course) the URLs listed above return 404 responses.
Any suggestions would be greatly appreciated.
Thanks!
The first issue shouldn't really happen. RedirectToAction should take into account your virtual directory path (you haven't hard coded an extra /foobar into your route setup have you?).
The second problem has nothing to do with MVC, it's just that you're using a vanilla HTML link that is pointing directly to the root of the server (that's what saying /blah implies). You should change your link to use one of the MVC helper methods to generate the URL instead to make sure it adds the virtual directory for you. So one of the following (the first is probably the best way, unless you need to heavily customise what the anchor tag looks like):
#Html.ActionLink("Widget Search", "Index", "WidgetSearch")
or
Widget Search

mvc 3 razor view engine using "default" views

i'm using Razor for a new project in my company, and i have been playing with it for 2 days and already found some weird behaviour imho.
I have a home controller in the root of my webapp and a home controller in an area , call it Area1. In both these controllers I have an Index action and therefore I got an Index view in the Views root folder, and another one in the Area1\Views folder. If I remove the index view within the area , so Area1\Views\Index.cshtml and I request Area1\Home\Index, I don't get an error about the view engine not finding the view for that action , but the "base" Index view is found in \Views\Index.cshtml and rendered.
Someone knows if this is a bug or it's done by purpose ? If so, there's any way to disable this default ?
Definitely NOT a bug.
This behaviour allows easy reuse of View templates, eg, for error handling.
The default behaviour for ASP.NET MVC is that if you do...:
return View();
...it searches for the view template with the name of the action to use in a number of places: the default naming convention folder for the controller, ie, /Area1/Views/Home/, then /Area1/Views/Shared/ then Area1/Views/ then /Views/Shared/ then /Views/
If it finds no such View matching the name of the action, then it throws an error.
So much for the default behaviour. To "customize" this behaviour, you just need to do the following:
In your controller actions, you can specify the name of the View template to use when you return. EG:
return View("MyOtherView");
or better still, if using T4MVC:
return View(MVC.Area1.Home.Views.MyOtherView); // does away with "magic" strings
As a result, I don't see that you need to switch off the default behaviour to be able to do (whatever) you want. Controllers are there to, uhmmmm, control what views are used to display to the user. That is the best practice.
However, ASP.NET MVC is very configurable, so there are ways and means, I presume, to switch this off.
If you want to do this, good luck to you, but it makes much more sense to follow the defaults and get to understand how ASP.NET MVC works, especially if you are a beginner.
NOTE:
The above applies to ASP.NET MVC 1, 2, 3 and will continue to do so. It is the default behaviour for all View engines, including Razor and WebForm Views.
PS:
And you can configure the urls using route registration if your concern is the look of the url in the user's browser.

Resources