I am new to MVC. I created an empty mvc application. Later added a view, a controller and a model for the view. After adding these three parts into the application, i tried running the application. But, i got "resource cannot be found" error from the browser. I cannot figure out the reason behind this.
Is there any settings to be modified to make it run properly? I did not change any settings.
Can anyone help me out of this?
Thanks
Manikandan
Add class config the default route and call it from global.asax
And of course add Home controller with action Index
public static class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
If all you've done is as you described, then the most likely cause for this is that you've used a Visual Studio template that doesn't include some scripts that are referenced in the auto-generated views you created.
Try opening the view that has this error (the view that you added) and remove its Scripts.Render directives, then run your app again and see if it goes away.
If it does go away using the above, then the problem is these missing scripts. In that case, you may either choose not to have them in your app or install them using nuget.
You probably didn't request the correct url in your browser or you didn't name your controller correctly.
Let's suppose that you wanted to add a HomeController.
Here are the steps:
Create a new ASP.NET MVC Empty Application
Add ~/Controllers/HomeController.cs
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
}
Add ~/Views/Home/Index.cshtml:
#{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<title>Index</title>
</head>
<body>
<div>
</div>
</body>
</html>
Run the application and navigate to /Home/Index
Related
I have been working on a project and I left it for about one week,when I came back to it and I tried to run the project with Ctr+F5 , it redirect to a URL which is not defined in route config. like this http://localhost:53771/Views/Home/Index.cshtml and confronted with HTTP 404: not found Error. while my route config is like this BUT IT WORKS FINE IF I TYPE THE CORRET URL MYSELF in front of the r+F5 , it redirect to a URL which is not defined in route config. like this http://localhost:53771
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default2",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
I started debugging the project and it seems the project never come to my HomeController . I just have one controller named Home, and when I removed all the codes of my Home controller it still act the same. and the other thing which it seems wrong, it creates the URL http://localhost:53771/Views/Home/Index.cshtml even before it comes to RegisterRoutes function. the problem is all local and here is my global.asax if needed. I appreciate any help
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
namespace ML_Projectname
{
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
MvcHandler.DisableMvcResponseHeader = true;
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
MvcHandler.DisableMvcResponseHeader = true;
}
protected void Application_PreSendRequestHeaders(object sender, EventArgs e)
{
HttpContext.Current.Response.Headers.Remove("X-Powered-By");
HttpContext.Current.Response.Headers.Remove("X-AspNet-Version");
HttpContext.Current.Response.Headers.Remove("X-AspNetMvc-Version");
HttpContext.Current.Response.Headers.Remove("Server");
}
}
}
I don't believe there is any issue with your routing or your application. Visual Studio's default start action (view by right clicking project -> selecting properties -> selecting Web) is "Current Page". If you're viewing a razor view (such as your /Views/Home/Index.cshtml) at the time you run the project in visual studio, visual studio will try to navigate to this resource directly. Directly requesting a razor view is not valid in the context of an MVC application and you end up seeing the 404 error.
To avoid this, you can set the start url to a specific URL (ex: http://localhost:53771) or even simpler, open and view one of the server side files (ex: HomeController.cs) before running your project via Visual Studio.
tldr version: this is an issue with a setting you have in visual studio, not an issue with your application code
I am trying to publish an MVC site. The website/application are both setup for .NET Framework 4. When the site is published, the MVC page gives the following error:
"The resource you are looking for has been removed, had its name changed, or is temporarily unavailable."
Also if I try to bring up a test aspx file, I get this error:
"The resource cannot be found."
The first test I perform is to delete the web.config. At this point, I attempt to bring up an MVC page again and same error is displayed:
"The resource you are looking for has been removed, had its name changed, or is temporarily unavailable."
However, if I try to bring up a test aspx page now, I actually get the page:
1 + 2 = 3
The test aspx page just contains this:
<html>
<body>
1 + 2 = <%=(1+2).ToString() %>
</body>
</html>
Any help of what to check next would be appreciated. Saw similar threads where it mentions of adding:
<customErrors mode="Off" />
and
<asp scriptErrorSentToBrowser="true"/>
But that still gives me the same error when bringing up a MVC page. Any help on how to proceed next to fix this MVC site would be appreciated. Thanks.
Update #1
I did a search for the keyword "routes.MapRoute" in the entire project and found it only once in the RouteConfig.cs:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
The page I am testing is:
http://.../Home/Index
This is the page that returns:
"The resource you are looking for has been removed, had its name changed, or is temporarily unavailable."
I found the problem! The problem was that the website hosting provider had the website setup for IIS 6.0. In that case there are two options:
Update hosting site to support IIS 7.0. This was my solution and solved the problem
Set the MapRoute to run a "fake" aspx page: (only do this if you cant update hosting to IIS 7.0):
// IIS 6.0
routes.MapRoute(
name: "Default",
url: "typehereanything.aspx/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Using this second method is not recommended since now you have to plug in "typehereanything.aspx" into every URL: http://.../typehereanything.aspx/Home/Index
Any of those two options solved the issue.
I would like to create link on my site, which, after click, would open download window (just some simple text file). In several tutorials I found a way to do it, however, for some reason, it seems that ActionLink doesnt call my method and looks for a view instead
My ActionLink
#Html.ActionLink("here is the gpx log", "Download", "Treks")
My Download method in Treks controller (added also following method using attribute routing in case it the case of the mess)
public FileResult Download()
{
byte[] fileBytes = System.IO.File.ReadAllBytes(#"~/Files/file.txt");
string fileName = "file.txt"; //I will add parameters later, once the basics work
return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);
}
[Route("treks/{trekname}")] //Route: /Users/12
public ActionResult ShowTrek(string trekname)
{
return View(trekname);
}
And this is the error I always get
The view 'Download' or its master was not found or no view engine supports the searched locations. The following locations were searched..
~/Views/Treks/DownloadFiles.aspx blahblahbla:
I spent one hour working on this and still not an inch closer to the solution. Does anybody know where I am making a mistake? Thanks a lot
Update: This is the content of my RouteConfig file
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
Edit: Ok, I debugged it. Seems the problem is in attribute routing. For some reason, controller ignored Download method and goes directly for ActionResult ShowTrek... any idea how to fix it?
Try to replace Fileresult with FileStreamResult
you may also need to create filestream object inside your method
new FileStream(fileName, FileMode.Open)
public FileStreamResult Download()
{
// Your code
}
Solved. Problem was in attribute routing. Pls see answer of Stephen Muecke in comments
I'm still new to ASP.NET MVC and I'm struggling a little with the routing.
Using the ASP.NET development server (running directly from Visual Studio), my application can find its views without any problems. The standard ASP.NET URL is used - http://localhost:1871/InterestingLink/Register
However, when I publish my site to IIS and access it via http://localhost/MyFancyApplication/InterestingLink/Register, I get a 404 error.
Any suggestions on what might be wrong?
More info...
This is what my global.asax file looks like (standard):
public class MvcApplication : System.Web.HttpApplication
{
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 = "" } // Parameter defaults
);
}
protected void Application_Start()
{
RegisterRoutes(RouteTable.Routes);
}
}
My controller is also very simple:
public class InterestingLinkController : Controller
{
public ActionResult Register()
{
return View("Register");
}
}
I figured out what was wrong. The problem was actually that IIS5 (in Windows XP) does not fire up ASP.NET when the URL does not contain a .ASPX. The easiest way to get around this is to add a '.aspx' to your controller section in global.asax. For example:
routes.MapRoute(
"Default",
"{controller}.aspx/{action}/{id}",
new { controller = "Home", action = "Index", id = "" }
);
Not pretty, but it will do.
Lots of things could be wrong:
Is the IIS Virtual Directory & Application set correctly?
Is the ASP.NET application being called at all? (Add some logging/breakpoiont in Application_Start and Application_BeginRequest)
Just for a start. You are going to have to apply the usual debugging approaches.
(To avoid issues like this, I rarely use the development server and just use IIS the whole time: most difficult thing is remembering to run VS elevated every time.)
I'd like to have dashes separate words in my URLs. So instead of:
/MyController/MyAction
I'd like:
/My-Controller/My-Action
Is this possible?
You can use the ActionName attribute like so:
[ActionName("My-Action")]
public ActionResult MyAction() {
return View();
}
Note that you will then need to call your View file "My-Action.cshtml" (or appropriate extension). You will also need to reference "my-action" in any Html.ActionLink methods.
There isn't such a simple solution for controllers.
Edit: Update for MVC5
Enable the routes globally:
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapMvcAttributeRoutes();
// routes.MapRoute...
}
Now with MVC5, Attribute Routing has been absorbed into the project. You can now use:
[Route("My-Action")]
On Action Methods.
For controllers, you can apply a RoutePrefix attribute which will be applied to all action methods in that controller:
[RoutePrefix("my-controller")]
One of the benefits of using RoutePrefix is URL parameters will also be passed down to any action methods.
[RoutePrefix("clients/{clientId:int}")]
public class ClientsController : Controller .....
Snip..
[Route("edit-client")]
public ActionResult Edit(int clientId) // will match /clients/123/edit-client
You could create a custom route handler as shown in this blog:
http://blog.didsburydesign.com/2010/02/how-to-allow-hyphens-in-urls-using-asp-net-mvc-2/
public class HyphenatedRouteHandler : MvcRouteHandler{
protected override IHttpHandler GetHttpHandler(RequestContext requestContext)
{
requestContext.RouteData.Values["controller"] = requestContext.RouteData.Values["controller"].ToString().Replace("-", "_");
requestContext.RouteData.Values["action"] = requestContext.RouteData.Values["action"].ToString().Replace("-", "_");
return base.GetHttpHandler(requestContext);
}
}
...and the new route:
routes.Add(
new Route("{controller}/{action}/{id}",
new RouteValueDictionary(
new { controller = "Default", action = "Index", id = "" }),
new HyphenatedRouteHandler())
);
A very similar question was asked here: ASP.net MVC support for URL's with hyphens
I've developed an open source NuGet library for this problem which implicitly converts EveryMvc/Url to every-mvc/url.
Uppercase urls are problematic because cookie paths are case-sensitive, most of the internet is actually case-sensitive while Microsoft technologies treats urls as case-insensitive. (More on my blog post)
NuGet Package: https://www.nuget.org/packages/LowercaseDashedRoute/
To install it, simply open the NuGet window in the Visual Studio by right clicking the Project and selecting NuGet Package Manager, and on the "Online" tab type "Lowercase Dashed Route", and it should pop up.
Alternatively, you can run this code in the Package Manager Console:
Install-Package LowercaseDashedRoute
After that you should open App_Start/RouteConfig.cs and comment out existing route.MapRoute(...) call and add this instead:
routes.Add(new LowercaseDashedRoute("{controller}/{action}/{id}",
new RouteValueDictionary(
new { controller = "Home", action = "Index", id = UrlParameter.Optional }),
new DashedRouteHandler()
)
);
That's it. All the urls are lowercase, dashed, and converted implicitly without you doing anything more.
Open Source Project Url: https://github.com/AtaS/lowercase-dashed-route
Here's what I did using areas in ASP.NET MVC 5 and it worked liked a charm. I didn't have to rename my views, either.
In RouteConfig.cs, do this:
public static void RegisterRoutes(RouteCollection routes)
{
// add these to enable attribute routing and lowercase urls, if desired
routes.MapMvcAttributeRoutes();
routes.LowercaseUrls = true;
// routes.MapRoute...
}
In your controller, add this before your class definition:
[RouteArea("SampleArea", AreaPrefix = "sample-area")]
[Route("{action}")]
public class SampleAreaController: Controller
{
// ...
[Route("my-action")]
public ViewResult MyAction()
{
// do something useful
}
}
The URL that shows up in the browser if testing on local machine is: localhost/sample-area/my-action. You don't need to rename your view files or anything. I was quite happy with the end result.
After routing attributes are enabled you can delete any area registration files you have such as SampleAreaRegistration.cs.
This article helped me come to this conclusion. I hope it is useful to you.
Asp.Net MVC 5 will support attribute routing, allowing more explicit control over route names. Sample usage will look like:
[RoutePrefix("dogs-and-cats")]
public class DogsAndCatsController : Controller
{
[HttpGet("living-together")]
public ViewResult LivingTogether() { ... }
[HttpPost("mass-hysteria")]
public ViewResult MassHysteria() { }
}
To get this behavior for projects using Asp.Net MVC prior to v5, similar functionality can be found with the AttributeRouting project (also available as a nuget). In fact, Microsoft reached out to the author of AttributeRouting to help them with their implementation for MVC 5.
You could write a custom route that derives from the Route class GetRouteData to strip dashes, but when you call the APIs to generate a URL, you'll have to remember to include the dashes for action name and controller name.
That shouldn't be too hard.
You can define a specific route such as:
routes.MapRoute(
"TandC", // Route controllerName
"CommonPath/{controller}/Terms-and-Conditions", // URL with parameters
new {
controller = "Home",
action = "Terms_and_Conditions"
} // Parameter defaults
);
But this route has to be registered BEFORE your default route.
If you have access to the IIS URL Rewrite module ( http://blogs.iis.net/ruslany/archive/2009/04/08/10-url-rewriting-tips-and-tricks.aspx ), you can simply rewrite the URLs.
Requests to /my-controller/my-action can be rewritten to /mycontroller/myaction and then there is no need to write custom handlers or anything else. Visitors get pretty urls and you get ones MVC can understand.
Here's an example for one controller and action, but you could modify this to be a more generic solution:
<rewrite>
<rules>
<rule name="Dashes, damnit">
<match url="^my-controller(.*)" />
<action type="Rewrite" url="MyController/Index{R:1}" />
</rule>
</rules>
</rewrite>
The possible downside to this is you'll have to switch your project to use IIS Express or IIS for rewrites to work during development.
I'm still pretty new to MVC, so take it with a grain of salt. It's not an elegant, catch-all solution but did the trick for me in MVC4:
routes.MapRoute(
name: "ControllerName",
url: "Controller-Name/{action}/{id}",
defaults: new { controller = "ControllerName", action = "Index", id = UrlParameter.Optional }
);