I have action:
public ActionResult Thumbnail(string image)
{
return GetThumbnail(image);
}
I am trying to access it with the next request:
http://localhost:60955/thumbnail/imagename.png
In config I have:
<add name="Png" path="/thumbnail/*.png" verb="GET" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
And in routes configuration:
routes.MapRoute(
name: "Thumbnail",
url: "thumbnail/{*image}",
defaults: new { controller = "Image", action = "Thumbnail" }
);
So it works for the above image URL. But I need this to work for any subfolder and the following returns 404:
http://localhost:60955/thumbnail/screenshots/imagename.png
And when I add slash it works again:
http://localhost:60955/thumbnail/screenshots/imagename.png/
Can I make it work with no trailing slash?
I feel like I need to customize the handler path in the config but cannot figure out how.
You should use query string for this case
URL change to this
http://localhost:60955/thumbnail?image=screenshots/imagename.png
Related
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();
}
I have a web site posting data to an old web app url mywebsite.com/Confirm.aspx
I am writing a new app using asp.net mvc and i would like that same url post to apply to an action called "Confirm" on my controller "Processor". Is it possible to do this using Routing in asp.net mvc? if so how?
There is a workaround.
You can add below request handlers in the web.config.
<system.webServer>
<handlers>
<add name="HtmlFileHandler" path="*.html" verb="GET" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
<add name="AspxlFileHandler" path="*.aspx" verb="GET" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>
Then you can create route definitions:
routes.MapRoute(
name: "LegacyHtml",
url: "{page}.html",
defaults: new { controller = "Home", action = "LegacyPage", page = UrlParameter.Optional }
);
routes.MapRoute(
name: "LegacyAspx",
url: "{page}.aspx",
defaults: new { controller = "Home", action = "LegacyPage", page = UrlParameter.Optional }
);
Next, you can either handle request in the controller action mentioned in routes.
Or you can further redirect the actions to other pages / sites based on value of Page path variable.
This is explained beautifully in this blog.
I'm working with a server with a custom URL: http://example.com/site/ and as you can see I need to change the default root path ("/") of my application to prevent 404 errors. Using IIS rewrite module with a outbound rule seems to work, with all html links and references being converted properly to query the internal website. The problem is when a Redirect() or RedirectToAction() method is used in my controllers, the internal name of the website is dropped so a 404 is caused. This is my IIS outbound rule:
<rewrite>
<outboundRules>
<rule name="Add path prefix to urls" stopProcessing="true">
<match filterByTags="A, Form, Img, Link, Script" pattern="^/(.*)" />
<action type="Rewrite" value="/site{R:0}" />
</rule>
</outboundRules>
</rewrite>
So to clarify: I have http://example.com/site/ when a redirection occurs to Account/Login, it becomes http://example.com/account/login instead of http://example.com/site/account/login . I guess the RouteConfig has to be tinkered with but I don't know how, or if I can do this in IIS. I have the following in my RouteConfig class:
routes.MapRoute(
name: "SiteRoot",
url: "site/{controller}/{action}",
defaults: new { controller = "Home", action = "Index" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}",
defaults: new { controller = "Home", action = "Index" }
);
Thank you.
Assuming the issue is only with pages protected by the authorize annotation, you probably need to update the /App_Start/Startup.Auth.cs file to change the login path. You're looking for LoginPath = new PathString("/Account/Login"), which you'll change to LoginPath = new PathString("/site/Account/Login"),. In the MVC 5 starter site I tested in this was on line #28.
In my testing with your redirect rule and route configuration normal RedirectToActions worked fine, but if you're having trouble you might try removing the "Default" route mapping so that URL structure isn't selected by the router when trying to redirect.
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.
Near the bottom of my routing registration, if a URL has a .js extension, I'd like to handle it with a particular controller (most .js content is served statically, but some is special and needs to be served via a controller). However, the following route is being skipped, and the catch-all route is handling the request.
routes.MapRoute("ContentScript", "{script}.js",
new { controller = "Content", action = "Script" },
new[] { "NameSpace.Controllers" }
);
What is the right way to do this?
In the route directly after that one every remaining request is routed like so (and this works and catches the .js files), so the issue is not in any part but the url parameter):
routes.MapRoute("ContentScript", "{*path}",
new { controller = "Content", action = "Index" },
new[] { "NameSpace.Controllers" }
);
I also tried the following, without success:
routes.MapRoute("ContentScript", "{*script}",
new { controller = "Content", action = "Script" },
new { script = new RegexConstraint("\\.js$") },
new[] { "NameSpace.Controllers" }
);
you have to add handler to the web.config so it can handle it ...
some thing like that :
<system.webserver>
<handlers>
<add name="scripts" path="*.js" verb="GET" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0"/>
</handlers>
</system.webserver>
stackoverflow link #1
stackoverflow link #2
The problem was an IgnoreRoute that I was unaware of (the RouteRegistry.cs file is 1,469 lines long... I have not studied it in its entirety, yet). js files are being handled in managed code--they were just being taken out by this before my route could handle the request.
routes.IgnoreRoute("{*path}",
new { path = new RegexConstraint(#"[^?]*\.(gif|jpe?g|png|ico|js|swf|css|txt|html?|xml|pdf)") }
);