asp vnext, ignore route - asp.net

i am using vnext and am using routes, but it routes EVERYTHING.
this is fine (from Startup.cs):
application.UseMvc(routes =>
{
// setup routes
// default mapping
routes.MapRoute(
name: "default",
template: "{controller}/{action}/{id?}",
defaults: new { controller = "Home", action = "Index" });
});
but then when i use (in views)
<link href='#Url.Content("~/CDN/r.css")' rel="stylesheet" />
or
<img src="/CDN/i.png" />
it gives a 404 error on those.
so how to set up ignore routes as in the previous versions?
thnx

You should register a StaticFiles middleware before MVC for your case where you want to serve static files like .css, .png etc. So the request for static files would be served by this middleware and would not reach MVC.
// Add static files to the request pipeline.
app.UseStaticFiles();
application.UseMvc(routes =>
{
// setup routes
// default mapping
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
You would need to add the package Microsoft.AspNet.StaticFiles in project.json to get it.

Related

WebApi Custom Routing in dotnet Core

In dotnet core, I'm trying to create a webapi endpoint that have a value before the controller name.
ex:
template: "api/{AccountId}/[controller]"
endpoint "api/435ABC/Reports/EndOfYear"
I've seen many examples on how to do this in MVC and in Framework 4.x, but not many with WebApi and where I set a parameter before the controller name.
In attribute routing you should change your controller route to [Route("api")] to accept all calls from https://example.com/api.
Note: it will affect all routes inside the Reports controller.
[Route("api")]
public class ReportsController : ApiController
and then decorate your action with route attribute like below:
[Route("{id}/[controller]/[action]")]
this way you can call your action method with https://example.com/api/435ABC/Reports/EndOfYear.
In convention-based routing you should only add route in UseMvc method and remove Route attributes from controller and action:
app.UseMvc(routes =>
{
routes.MapRoute(name: "default", template: "api/{controller=Home}/{action=Index}"); // this line specifies default route
routes.MapRoute(name: "Reports", template: "api/{id}/{controller=Reports}/{action=EndOfYear}"); // this line specifies your custom route
});

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.

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

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.

MapPageRoute redirecting wront pages

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.

Resources