I have an mvc application running on IIS 7.0 on windows vista.The appication is redirecting to the proper controller and action.But i am getting an error saying view is not found in the path, when the view is present at the particular path.
Route is as shown below.
routes.MapRoute(
"Default", // Route name
"home/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter constraints
);
I am getting the error as The view 'Index' could not be located at these paths: ~/Views/Home/Index.aspx, ~/Views/Home/Index.ascx, ~/Views/Shared/Index.aspx, ~/Views/Shared/Index.ascx when i run the mvc application http://localhost/mvcsf/Home/
I had to reconfigure IIS to handle MVC apps. Check this one out as well:
MVC Config on IIS v6.0
Try something like this, it seems like it lies in the Windows features that comes with IIS 7 :
http://blogs.dovetailsoftware.com/blogs/kmiller/archive/2008/10/07/deploying-an-asp-net-mvc-web-application-to-iis7.aspx
The choice of view is defined by the controller. What does the home controller do for the Index action? If this is the vanilla site generated by the system, then it is expecting to find "~/Views/Home/Index.aspx", via the controller's action (below). So: does this index page exist?
public ActionResult Index()
{
ViewData["Title"] = "Home Page";
ViewData["Message"] = "Welcome to ASP.NET MVC!";
return View();
}
(the default view has the pattern {controller}/{action}; you can specify other views via overloads on View(...))
Related
There is a long-standing issue with ASP.NET MVC not properly handling URL routes if there are forward slashes as a parameter even if they are URL encoded.
Example:
On a default install, go to this URL (adjusting ports as needed)
http://localhost:11541/Token/Create?
callback=http%3a%2f%2flocalhost%3a11491%2ftoken%2fcreatetoken%2fAddPrivateValues
Notice that the Controller is "Token" and "Create" is the method to be called. This is the error I get:
The view 'http://localhost:11491/token/createtoken/AddPrivateValues' or
its master was not found or no view engine supports the searched
locations. The following locations were searched:
~/Views/Token/http://localhost:11491/token/createtoken/AddPrivateValues.aspx
~/Views/Token/http://localhost:11491/token/createtoken/AddPrivateValues.ascx
~/Views/Shared/http://localhost:11491/token/createtoken/AddPrivateValues.aspx
~/Views/Shared/http://localhost:11491/token/createtoken/AddPrivateValues.ascx
Notice that it's calling "CreateToken/AddPrivateValues". This is wrong. It should be calling Token.Create.
This issue appears to have been broken since 2009 (according to prior S.O. research) so I'm not holding my breath. I just need to fix this and move it to Azure.
I tried adding a {*id} route to my controller, but that doesn't work because there are many forward slashes. The only way to fix this is to disable this parsing in the machine.config (web.config WILL NOT WORK)
Question
How do I set this property in Windows Azure, without using RDP so that the permissions on Web.config are as secure and locked down as they were before I attempted to do this?
I am unable to replicate your issue on a clean deployment of an MVC 4 project. How are you trying to pass the callback parameter back to your view (if at all). If you are using
return View(callback);
This would be why it is failing, because it is interpreting the string variable as a view name. Try either storing it in a view bag or encapsulating it within a view model. As a side note, I know that passing parameters with slashes seems to work in all MVC versions since on the Account/Login controller action you can send a returnUrl without issue.
Below is my configuration, let me know if I have deviated anywhere from your setup.
Route Config
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//Changed default route to allow me to only have to create one controller
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Token", action = "Index", id = UrlParameter.Optional }
);
}
Token Controller
public ActionResult Index()
{
return View();
}
public ActionResult Create(string callback)
{
#ViewBag.Item = callback;
return View();
}
Create.cshtml
#{
ViewBag.Title = "Create";
}
<h2>Create</h2>
#ViewBag.Item
My URL: http://localhost:11319/Token/Create?callback=http%3a%2f%2flocalhost%3a11491%2ftoken%2fcreatetoken%2fAddPrivateValues
My Create Page Results
<h2>Create</h2>
http://localhost:11491/token/createtoken/AddPrivateValues
<script src="/Scripts/jquery-1.8.2.js"></script>
This is a beginner level question for asp.net MVC
I have the following code in global.asax.cs
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 = (string)null } // Parameter defaults
);
}
in Homecontroller.cs i have updated the Index method as follows
public ActionResult Index(string id)
{
ViewData["Message"] = "Welcome to ASP.NET MVC1!"+ id;
return View();
}
My understanding is, if I give the url http://localhost/mvc1/default/1 it should work
instead it is throwing up 404 error
any help what is the reason behind this
I'm assuming your application is called "mvc1" and that's the root of your project. If that's the case:
So "default" is the name if your route, not the name of the action. Basically what the routing engine does is look for a controller and action that matches requests coming in. Given the route you have setup, it would break down like this:
http://localhost/MVCApplication1/default/1
(cont) (action)
If certain parts of the route are omitted, it will attempt to fill in the missing values with the defaults you have specified. As you can see, there is no controller named DefaultController in your project, and thus it uses the default you've specified which is Home. It then tries to find an action method called default and fails again, so it uses the default value in your route, which is Index. Finally, you have 2 segments left in your URL, and no route matches that pattern (2 segments after the action), so it can't find the right place to go.
What you need to do is remove one of your segments, and this should work. Routing can be a little tricky, so I would recommend reading up on it.
The URL you're requesting is asking for a controller called "mvc1" and an action called "default" which will receive an id of "1". Since you don't have a controller named "mvc1" (I assume?), you're getting the 404 error.
The defaults for controller and action are only used if controller and action aren't provided. Since you provided controller and action, MVC is looking for them specifically.
i created a new solution and it builds fine targeting framework 4.0 but when i run it, my browser comes up saying:
The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Requested URL: /
any ideas on how to debug this?
Try adding the default.aspx page that comes with the asp.net mvc 1.0 project template. I had a similar issue running mvc 2 out of the box on a computer with IIS 5 (XP), and that did the trick.
Default.aspx:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="YourNamespace.Website.Default" %>
<%-- Please do not delete this file. It is used to ensure that ASP.NET MVC is activated by IIS when a user makes a "/" request to the server. --%>
Default.aspx.cs:
using System.Web;
using System.Web.Mvc;
using System.Web.UI;
namespace YourNamespace.Website
{
public partial class Default : Page
{
public void Page_Load(object sender, System.EventArgs e)
{
// Change the current path so that the Routing handler can correctly interpret
// the request, then restore the original path so that the OutputCache module
// can correctly process the response (if caching is enabled).
string originalPath = Request.Path;
HttpContext.Current.RewritePath(Request.ApplicationPath, false);
IHttpHandler httpHandler = new MvcHttpHandler();
httpHandler.ProcessRequest(HttpContext.Current);
HttpContext.Current.RewritePath(originalPath, false);
}
}
}
You don't need to add the default.aspx page described above.
The browser will display this 404 message if you add and run a new Empty ASP.NET MVC 2 application "out of the box".
This is because of the default route that is defined in global.asax.
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
You can see it is looking for a controller called Home and an action called Index.
When creating a new empty project it's left to you to create the Home controller and Index action (they are not there in an empty project), then create the view for the Index action too.
My guess is that you need to reregister or enable the framework under IIS.
Try running aspnet_regiis from the appropriate framework tree and / or make sure that the proper framework version is allowed under IIS web extensions.
I've got the following routes:
// Submission/*
routes.MapRoute(
"Submission",
"Submission/{form}",
new { controller = "Submission", action = "Handle", form = "" });
// /<some-page>
routes.MapRoute(
"Pages",
"{page}",
new { controller = "Main", action = "Page", page = "Index" });
The first routes requests exactly as per this question. The second generically routes a bunch of static content pages. For instance localhost/Help, localhost/Contact, etc. all route to the MainController which simply returns the view according to the page name:
public class MainController : Controller
{
public ActionResult Page()
{
var page = (string)RouteData.Values["page"];
return View(page);
}
}
The problem is, during testing at least, localhost/ gives a dir listing instead of routing to Main/Index.aspx. The real problem is it fubars my SiteMap menu because the URLs aren't matching what's defined in the Web.sitemap file. localhost/Index does give me the correct view, however.
The curious thing is this works as expected on Mono / XSP.
If you are testing it using Visual Studio Dev Server than it should work. I have tried it just now.
On IIS neither of "localhost/" and "localhost/Index" should work unless you enabled wildcard mapping
So it works for me. You probably are missing something that is not obvious from the post.
BTW, your action can be improved:
public ActionResult Page(string page)
{
return View(page);
}
EDIT: Here is my sample project.
I finally figured it out. There were (potentially) two things going awry. One, the project must have the MVC project type GUID. Check out this for an idea - though the post isn't quite on topic. Two, Visual Studio 2008 requires SP1 for an updated ASP.NET development server; the version of it prior to SP1 doesn't kick off Global.asax w/o a Default.aspx page.
I need the correct Global.asax settings in order for my Dynamic Data site to run under an ASP.NET MVC project. The routing currently appears to be my issue.
Here is my global.asax:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
MetaModel model = new MetaModel();
model.RegisterContext(typeof(Models.DBDataContext), new ContextConfiguration() { ScaffoldAllTables = true });
routes.Add(new DynamicDataRoute("DD/{table}/{action}.aspx") {
Constraints = new RouteValueDictionary(new { action = "List|Details|Edit|Insert" }),
Model = model
});
routes.MapRoute(
"Assignment",
"Assignment/{action}/{page}",
new { controller = "Assignment", action = "Index", page = "" });
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Admin", action = "Index", id = "" }); // Parameter defaults
}
Link that I'm trying to use is:
http://localhost:64205/DD/Work_Phases/ListDetails.aspx
I am getting the following message:
Server Error in '/' Application. The
resource cannot be found. Description:
HTTP 404. The resource you are looking
for (or one of its dependencies) could
have been removed, had its name
changed, or is temporarily
unavailable. Please review the
following URL and make sure that it is
spelled correctly.
Requested URL:
/DD/Work_Phases/ListDetails.aspx
I've tried replacing DD with DynamicData since the folder inside of the app is DynamicData and that yielded the exact same result.
The URL
http://localhost:64205/DD/Work_Phases/ListDetails.aspx
is matching your second (default) route, which is trying to hit a controller called "DD".
You may need another route entry that looks something like this:
routes.MapRoute(
"DD",
"DD/{action}/{page}",
new { controller = "NameOfController", action = "Index", page = "" }
);
...although I can't imagine why you would need to pass a page parameter. The page view that is hit depends on the return action of the controller method.
For a better look at integrating Dynamic Data with ASP.NET MVC, have a look at Scott Hanselman's Plugin-Hybrids article. He has some details about handling the .ASPX files that are not part of MVC. In particular, if you have an .ASPX that you don't want to be processed by the ASP.NET MVC controllers, you can install an Ignore Route:
routes.IgnoreRoute("{myWebForms}.aspx/{*pathInfo}");
It should be noted that ASP.NET MVC is configured out of the box to ignore URL requests for files that physically exist on the disk, although Scott's IgnoreRoute technique is apparently more efficient.
The url doesn't match your dynamic data route because it doesn't fit the constraints you put on it. You're requesting action ListDetails but only these actions are allowed
Constraints = new RouteValueDictionary(
new { action = "List|Details|Edit|Insert" }
EDIT: are you sure that an action called ListDetails exists? Then modify the constraints above to
Constraints = new RouteValueDictionary(
new { action = "ListDetails|List|Details|Edit|Insert" }
Just to be sure that it's the constraints that's causing the route to be ignored, can you try one of the default actions? E.g.
http://localhost:64205/DD/Work_Phases/List.aspx
For ASP.NET MVC to work, you will have to match the URL you are trying to access with the list of routes.
For your current global.asax, example of valid URLs are:
http://domain/AnyController/AnyAction/AnyParameter
http://domain/Assignment/
http://domain/Assignment/AnyAction/AnyParameter
MVC requests are redirected to the proper Controller class, Action method, with parameters as passed in. MVC request is not redirected to any ASPX class. This is the difference between ASP.NET MVC and vanilla ASP.NET Page.