Server Error in '/' Application asp web api - asp.net

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}");

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 }
);

ASP.NET Core MVC route to controller any child url like "admin/*"

I want to route any url address like /admin/* as my admin is on Angular2 and uses its routing. I tried template: "admin/{*url}"though it works not only with /admin url but with any other. Is there any way to solve this problem?
Add this code in your WebApiConfig.cs file :
config.Routes.MapHttpRoute(
name: "some name",
routeTemplate: "admin/{*url}",
defaults: new { controller = "Admin", action = "Your default action here" });
Set the default controller in defaults.

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.

Resources