i have a mixed aspx/MVC webapp project and need to rewrite incoming URL's either in the MVC routing or through IIS rewriting. whatever works. I cannot figure this out.
I have the following OLD path:
/Article/Nugget/Article.aspx?articleId=30
and i need to rewrite this to:
/Article/Nugget/30
The issue is the MVC route is reading in the Article.aspx being passed as a parameter and anything i do to rewrite this in IIS7 is being ignored. Well.. the issue is i don't have a clue :)
Try something like:
routes.MapRoute(
"Article",
"Article.aspx",
new { controller = "Article", action = "Nugget"}
);
With a parameter named articleId in your action method of course
public ActionResult Nugget(int articleId)
{
..
}
Related
I have a project that I recently upgraded to ASP.NET MVC 3. On my local machine, everything works fine. When I deploy to the server, I get an error anytime I use a RedirectToAction call. It throws a System.InvalidOperationException with the error message No route in the route table matches the supplied values. My assumption is that there is some configuration problem on the server, but I can't seem to be able to figure it out.
I ran into this with areas within MVC3 when redirecting across areas. As others have said, Glimpse is very useful here.
The solution for me was to pass in the Area within the route values parameter changing:
return RedirectToAction("ActionName", "ControllerName");
to:
return RedirectToAction("ActionName", "ControllerName", new { area = "AreaName" });
I had a similar problem once with RedirectToAction and found out that you need a valid route registered that leads to that action.
Check out glimpse and see if you can get some route debugging information:
http://getglimpse.com/
You could add a route table to your RouteConfig.cs file like below:
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapMvcAttributeRoutes();
var namespaces = new[] { typeof(HomeController).Namespace };
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute("name", "url", new { controller = "controllerName", action = "actionName" }, namespaces);
}
NB: the "url" is what you'd type into the address bar say: localhost:/home
After setting up the route, use RedirectToRoute("url").
Or if you'd prefer the RedirectToAction() then you don't need to set up the above route, use the defaults.
RedirectToAction(string action name, string controller name);
I hope this helps.
There's a difference with trailing slashes in routes not working with MVC 3.0. MVC 2.0 doesn't have a problem with them. I.e., if you change the following:
"{controller}.mvc/{action}/{id}/"
to:
"{controller}.mvc/{action}/{id}"
it should fix this (from this thread, worked for me). Even when you use the upgrade wizard to move to MVC 3.0, this still throws InvalidOperationException. I'm not aware whether this is what Schmalls was talking about though.
In my case, default route was missing:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
I am fairly new to asp.net and I have a starting URL of http://localhost:61431/WebSuds/Suds/Welcome and routing code
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{page}", // URL with parameters
new { controller = "Suds", action = "Welcome", page = 1 } // Parameter defaults
);
routes.MapRoute(
"Single",
"{controller}/{action}",
new { controller = "Suds", action = "Welcome" }
);
I am receiving the following error: 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.
Can anyone please help me figure out how to route the beginning url to the controller.
Because the first part of your URL is WebSuds, the framework is trying to map the request to WebSudsController instead of SudsController. One thing you can try is to change the url parameter of your route to "WebSuds/{controller}/{action}".
Route tables are searched on a first come first served basis. Once an appropriate match is found any following routes will be ignored.
You need to add "WebSuds/{controller}/{action}" and put it above the default route. The most specific routes should always go above the more generic ones.
With the following Routes and Url you have given make sure you have these Methods in your Controller
public class SudsController
{
public ActionResultWelcome(int? page)
{
return View();
}
}
Since you have created two routes of the same Action, one with a parameter page and another without. So I made the parameter int nullable. If you don't make your parameter in Welcome nullable it will return an error since Welcome is accepting an int and it will always look for a Url looks like this, /WebSuds/Suds/Welcome/1. You can also make your parameter as string to make your parameter nullable.
Then you should have this in your View Folder
Views
Suds
Welcome.aspx
If does doesn't exist, it will return a 404 error since you don't have a corresponding page in your Welcome ActionResult.
If all of this including what BritishDeveloper said. This should help you solve your problem.
I have an ASP.NET MVC application where the default page should be index.html which is an actual file on disk.
I can browse to the file using www.mydomain.com/index.html so I know it will be served and exists but if I use www.mydomain.com I get a 404.
I have ensured that the default document is correctly set in IIS7 and I have even gone so far as to commented out all the routes defined in my global.asax to ensure I don't have a route causing this problem.
So to summarize:
I have an index.html file on disk and IIS7 is set to use index.html as the default document.
If I remove my ASP.NET MVC application and leave the index.html file is served as the default document as expected.
If I publish my ASP.NET MVC application then the index.html doesn't get served by default document.
Does anyone know how to get ASP.NET MVC to serve the default document?
ASP.Net MVC routing is controlled by the Global.asax / Global.asax.cs files. The out-of-the-box routing looks like this:
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 = UrlParameter.Optional } // Parameter defaults
);
}
When no path is specified, ie. www.domain.tld/, the route is the empty string, "". Therefore the routing looks for a controller with that name. In other words, it looks for a controller with no name at all. When it finds no such controller it serves up a 404 NOT FOUND error page.
To solve the problem, either map that path to something meaningful or else ignore that route entirely, passing control over to the index.html file:
routes.IgnoreRoute("");
I had a similar problem with a WebForms application. In your web.config make sure the resourceType attribute of the StaticFile handler under system.webServer is set to Either.
<add name="StaticFile" path="*" verb="*" type="" modules="StaticFileModule,DefaultDocumentModule,DirectoryListingModule" scriptProcessor="" resourceType="Either" ...
I found a way around this. If you want index.html to be in the root of your MVC application (i.e next to your controller/model/view/appdata etc folders), you can do this:
Say you have home.html, aboutus.html and contactus.html.
//this route matches (http://mydomain.com/somekindofstring)
routes.MapRoute(
"SingleRootArg",
"{id}",
new { controller = "Home", action = "Details", id=""});
// so now that you have your route for all those random strings.
// I had to do this for vanity urls. mydomain.com/ronburgandy etc. however,
// mydomain.com/index.html will also come in just how you'd expect.
//Just an example of what you COULD do. the user would see it as root html.
Public ActionResult Details(string id)
{
//let me save you the trouble - favicon really comes in as a string *ANGER*
if(String.IsNullOrEmpty(id) || id.ToLower().Contains("favicon.ico"))
return Redirect("~/index.html");
if(id == "aboutus.html")
return Redirect("~/aboutus.html");
if(id == "contactus.html")
return Redirect("~/contactus.html");
if(id == "index.html")
return Redirect("~/index.html");
}
index.html aboutus.html index.html are now at the same level as my CSPROJ file.
Sorry for resurrecting this mummy, but i don't believe this issue was ever a default document issue. In fact you probably don't want to have a default document set as many of the other answerers have stated.
Had this problem as well, a similar problem. the cause of my issue was that the Application Pool for the site was set to use .NET Framework v2 and should have been set to v4. once I changed that it loaded correctly.
You could ignore the route in your MVC application and let IIS serve it.
routes.IgnoreRoute("index.html")
etc
I suspect you added index.html yourself as that extension would be unknown to the mvc framework.
Your default index page in mvc is //domain/home/index and is physically index.aspx.
If you call your domain using //domain then the routing engine will assume /home/index.aspx and not index.html.
How can I have different URL ids like www.somewebsite.com/index?theidentifier=34 only in ASP.NET MVC not Webforms.
Well, for what purpose? Just to access the value? All querystring values can be routed to params in the action method like:
public ActionResult index(int? theidentifier)
{
//process value
}
Or, you can use the QueryString collection as mentioned above, I think it's via this.RequestContext.HttpContext.Request.QueryString.
If you want to handle your routing in ASP.NET MVC, then you can open Global.asax and add calling of routes.MapRoute in RegisterRoutes method.
The default routing configuration is {controller}/{action}/{id} => ex:
http://localhost/Home/Index/3 , controller = HomeController, Action=About, id=3.
You may add something like :
routes.MapRoute(
"NewRoute", // Route name
"Index/{id}", // URL with parameters
new { controller = "Home", action = "Index",id=1 } // Parameter defaults
);
so http://localhost/Index/3 will be accepted
Remember to add these code above the default route configuration, because ASP.NET will search for the first matching route
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.