MVC4 Why can't route as .html file? - asp.net

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.

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

the resource cannot be found asp.net mvc

hi i already tried some solution available on stack overflow...some of them are configuring in routeconfig, going to project properties and creating a virtual directory. But none of them work for me. I've been working on this project and i don't know what happened suddenly. I've tried restarting laptop as well as visual studio . Please help it was working fine before
UPDATE:i have 2 controllers that are not working properly admin and account controller both have views as well as their corresponding controller
Remove all your existing routes and just use the default route:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
This should keep things simple and should also work with your existing controller/actions.
I think the issue is to do with your routes. Can you try an explicit route like the one below and check that it works?
routes.MapRoute(
name: "AdminIndex",
url: "admin/index",
defaults: new { controller = "Admin", action = "Index" }
);
Can you try adjusting your admin route like so:
routes.MapRoute(
"Admin",
"{controller}/{action}/{id}",
new { action = "Index", controller = "Admin", id = UrlParameter.Optional }
);

Server Error in '/' Application asp web api

I am working on ASP web api with angularjs. I want to load index.html as the default page. But whenever '/' route is entered, 404 resource not found error is thrown. Instead when I enter the URL '/index.html' page can be viewed. I want to load index.html page when '/' route is entered.
RouteConfig.cs file is as follows - `
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } );`
app.js file - `$routeProvider.when('/', { template: '<div></div>', controller: '' })`
Adding the following line before routes.MapRoute might help you
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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.

Resources