MapPageRoute redirecting wront pages - asp.net

I have a ASP.NET MVC3 Razor project. I wanted to add aspx page to this project and I found that it is possible by using MapPageRoute. I added it to my GlobalAsax.RegisterRoutes and it's redirecting me to my aspx page. But also most of the old pages are also redirected to this - they;re getting URL like this:
http://localhost:61000/AEM/Report?action=EditUser&controller=Settings
My RegisterRoutes method:
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute(
"TelerikReport",
"AEM/Report",
"~/WebForms/TelerikReport.aspx", true
);
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Account", action = "LogOn", id = UrlParameter.Optional } // Parameter defaults
}
What am I doing wrong here?
Edit:
I noticed that it's redirecting pages wrong when I use "return RedirectToAction(...)" in my controllers.

Try to modify your code like:
routes.MapPageRoute(
"TelerikReport",
"AEM/Report/{*queryvalues}",
"~/WebForms/TelerikReport.aspx", true
);
Though I am not sure, but you can give it a try. As explained Here.

Related

Redirecting a controller and action method to its URL

I have a project that I am working on that was done in ASP.NET MVC 5.
They used the default URL structure that comes standard with the framework. For example, the AGM page was constructed like this:
Controller: Home
Action method: AGM
Without any routing setup, to access this page, you would need to go to the following URL:
www.example.com/Home/AGM
This is the URL that they sent to the press. This URL looks horrible and I want to implement a cleaner URL structure which will look like this:
www.example.com/agm
I have set it up like this in the RouteConfig.cs file:
routes.MapRoute(
name: "AGM",
url: "agm",
defaults: new { controller = "Home", action = "AGM" }
);
What I want to achieve is if the user types in www.example.com/Home/AGM then it needs to display the URL like www.example.com/agm. Instead, it displays like www.example.com/Home/AGM.
I'm not sure how to implement this?
Configure routing and define action in the url
routes.MapRoute(
name: "AGM",
url: "{action}",
defaults: new { controller = "Home", action = "AGM" }
);
Then in the AGM method redirect to www.example.com/agm
if (Request.Path.ToLower() == "/home/agm")
{
return Redirect("/agm");
}
You can simply add [Route("AGM")] and [Route("Home/AGM")] top of your Action method:
[Route("AGM")]
[Route("Home/AGM")]
public IActionResult AGM()
{
return View();
}

How to redirect the main URL to new path in ASP.NET MVC

I am using ASP.NET MVC, In localhost, using the web, I changed the start URL option it works fine there, once I published it to the main domain, it is not working on that. I hope anyone can help me. I want to redirect from
domain.com/
to
domain.com/Home/Index
Thanks a lot!
you need to change your default route as this
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
on startup the default route is initialized and hence redirected to the above website as
domain.com/Home/Index

MVC4 Why can't route as .html file?

I'm use visual studio 2012 with MVC4.
I want some web page looks like .html suffix. So I add this code on RouteConfig:
routes.MapRoute(
name: "Static",
url: "{controller}/{action}/{id}.html",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
But the problem is I can't access it, the page always returns 404, and shows message:
the resource is not found or removed.......
Is there any way can let it route as html page? thanks
Disable the handler for the .html extension.

ASP.Net MVC No routes but default are working

I have added a new Controller to my application, and then from there added in a new View (right click, add view on the context menu)
I have added the route to the Route table, but when I browse to it I get a 404.
I can only assue that this is something fundamental that is wrong, as if I replace the values in the default route, I still cannot access my page.
The route table is;
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Classroom",
"{controller}/{action}/",
new { controller = "Classroom", action = "Index" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
//defaults: new { controller = "Classroom", action = "Index", id = UrlParameter.Optional }
);
there is only a return View(); in the index method. I have installed Glimpse, but as I am getting a 404, I cannot actually see the url that is being passed back.
So, what techniques / tricks are there I can use to track this down, (and any common gotcha's that could be causing this?)
Visual Studio 2012, using the inbuilt webserver.
This is Fixed. It was a stupid Gotcha on my part. The Controller was Called Classroom not ClassroomController
I have added the route to the Route table
No, you don't need to add any route for that. Get rid of the Classroom route from your Global.asax and leave only the Default one. Now you could safely navigate to your controller using /classroom/index which will render the Index action of the ClassroomController.

Wrong Page.GetRouteUrl() in asp.net 4.0 application. Any help for URL routing

I am creating asp.net web application with web forms, I am using register routes to product friendly URL's.
Following is the code in Global.asax.cs;
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
RegisterRoutes(RouteTable.Routes);
}
void RegisterRoutes(RouteCollection routes)
{
// Register a route for Categories/All
routes.MapPageRoute(
"All Categories", // Route name
"Categories/All", // Route URL
"~/AllCategories.aspx" // Web page to handle route
);
// Route to handle Categories/{CategoryName}.
// The {*CategoryName} instructs the route to match all content after the first slash, which is needed b/c some category names contain a slash, as in the category "Meat/Produce"
// See http://forums.asp.net/p/1417546/3131024.aspx for more information
routes.MapPageRoute(
"View Category", // Route name
"Categories/{*CategoryName}", // Route URL
"~/CategoryProducts.aspx" // Web page to handle route
);
// Register a route for Products/{ProductName}
routes.MapPageRoute(
"View All Product", // Route name
"Products", // Route URL
"~/ViewProducts.aspx" // Web page to handle route
);
// Register a route for Products/{ProductName}
routes.MapPageRoute(
"View Product", // Route name
"Product/{ProductName}", // Route URL
"~/ViewProduct.aspx" // Web page to handle route
);
// Register a route for Products/{ProductName}
routes.MapPageRoute(
"Add Product", // Route name
"NewProduct", // Route URL
"~/AddProduct.aspx" // Web page to handle route
);
}
Now in one page when I put
lnkNewProduct.NavigateUrl = Page.GetRouteUrl("Add Product");
It produce wrong href url when I run the project.
Can anyone please tell why this happen? At moment it is showing URL like http:\localhost:5770\Categories\All?Length=11... this is hard to understand.
Any hint or help???
Thanks
You need to use the correct overload:
Page.GetRouteUrl("Add Products", null);
This GetRouteUrl overload uses the RouteName.
You can use Route Url expressions also:
http://msdn.microsoft.com/en-us/library/system.web.compilation.routeurlexpressionbuilder.aspx

Resources