I have created the following MapRoute function. And it's being called right from Application_Start() in Global.asax.
public static class RouteConfig
{
public static void RegisterRoutes( RouteCollection routes )
{
routes.MapRoute(
name: "TestMe",
url: "TestMe.axd",
defaults: new { controller = "Access", action = "SignOn" }
);
}
}
While I can access the specific controller if I use
http://localhost/TestSite/Access/SignOn,
I can't access it with this URL.
http://localhost/TestSite/TestMe.axd.
Can someone please point out what I am missing here?
Many thanks!!
Found some information from this link
I added
<modules runAllManagedModulesForAllRequests="true" />
into my Web.config and it's working now.
Related
So I'm following the get started tutorial on ASP.NET . So far so good, however going to /controllerName is quite tedious. Is there a way how I can go to that index page on start up of the project? I couldn't find it in the walkthrough or any documentation. I'm using version 6.0 . I'm sorry for this basic question.
.NET MVC has built in defaults that can be overriden.
The default start page which will be routed from the root / is the Index action on the home controller:
public class HomeController :
{
public IActionResult Index()
{
// ...
}
}
Sticking to these defaults is a good idea so other developers will find it easier to make sense of your project.
But you can override the defaults in .NET 5.0 if you wish in the Startup.cs Configure method:
app.UseEndpoints(routes =>
{
routes.MapControllerRoute(
name: "default",
pattern: "{controller=MyDefaultController}/{action=MyDefaultActionMethod}/{id?}");
});
}
You can change the desired starting page from Route config. It's in
App_Start>RouteConfig
route config
You can change it controller name and index page as you want.
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controllerName}/{actionName}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
We know route is register in route.config file like below
routes.MapRoute(
name: "ProductPage",
url: "{productId}/{productTitle}",
defaults: new { controller = "Products", action = "Show" },
constraints: new { productId = "\\d+" }
);
Can we delete routing related code from route config file and implement attribute routing instead?
See this
[Route("{productId:int}/{productTitle}")]
public ActionResult Show(int productId) { ... }
Can we use the above attribute routing instead?
Can we delete all routing related code from route config file, so my route config would look like?
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
}
}
Please help me to have control over this routing issue. Thanks
You certainly can, but it's better to keep one default route in RouteConfig and add decorate other action methods with route attributes.
I have problems accessing a route, if it contains a dot. For reproducing, create a default MVC4 WebApi application. Take the default route...
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
write the controller as follows...
public class HomeController : Controller
{
public ActionResult Index(string id)
{
if (id == null)
{
return View();
}
else
{
return PartialView(id);
}
}
}
and create two files "test.cshtml" and "test.menu.cshtml" next to the existing "index.cshtml".
Opening /home/index/test works as expected. It brings back the contents of "test.cshtml".
However, opening /home/index/test.menu brings back a 404 error.
Why does this happen? And how can it be avoided?
The 404 error that you are seeing is from IIS. The module for MVC routing is never getting control.
Adding the following option to the modules section of the <system.webServer> section in web.config will make sure that the MVC routing gets attempted and should fix your problem:
runAllManagedModulesForAllRequests="true"
in my web.config there was no existing modules section so I added the following line to the <system.webServer> section:
<modules runAllManagedModulesForAllRequests="true" />
And now it works for me.
I have an MVC application and am using the standard routeconfig that routes /Controller/Action/Id
I want it to additionally capture /Controller/Action.html as the url and as well and point to /controller/action also.
I am using a jquery library that I have no control over, and a function requires a url that points to a webpage or an image. However, it doesn't appear to understand that ends without an extension(.html, .php etc) is a link to html and throws an error.
Edit: I tried as the commenter below suggested, and still can't seem to get it to work. Here is my route config.
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute("routeWithHtmlExtension",
"{controller}/{action}.html",
new { controller = "Home", action = "Index" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
This Url works:
http://localhost:14418/Album/Test
This one does not:
http://localhost:14418/Album/Test.html
In web.config
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
...
</system.webServer>
If you set up the following route, it will work:
routes.MapRoute("routeWithHtmlExtension",
"{controller}/{action}.html",
new {controller = "Home", action = "Index" }
);
I am working on a blog site, upgrading it to MVC3.
I have this in web.config:
<add name="RssHandler" verb="*" path="rss.axd" type="Blog.Web.RSSHandler, Blog.Web"/>
So, if I access the rss.axd directly from the url, it works:
http://mydomain/rss.axd
Now I want to use http://mydomain/RSS to map the rss.axd, so in the global.asax, i tried
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute("RSS-View", "RSS", "~/rss.axd", false);
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
...
}
but i got a 404 error, because the rss.axd is not a physical file that exists in the website folder.
In the past, i use UrlRewrite to do this and it works, like this:
<LookFor>~/RSS<LookFor>
<SendTo>~/rss.axd</SendTo>
So, in MVC3 is there anyway to do this "ReWrite", not "Route"? I don't want to use redirect.
There's no built-in support available for routing to http handlers.
One possibility is to write a custom route handler (IRouteHandler) that will return an instance of your RSSHandler:
public class RssRouteHandler : IRouteHandler
{
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
return new Blog.Web.RSSHandler();
}
}
and finally you could register this route:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.Add("RSS-View", new Route("rss", new RssRouteHandler()));
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
Now we are no longer going through the virtual rss.axd address. When a request is made to /rss we directly serve this request through our custom route handler, as if we had the following in web.config:
<add name="RssHandler" verb="*" path="rss" type="Blog.Web.RSSHandler, Blog.Web"/>