Unable to map route for robots.txt in asp.net mvc - asp.net-mvc

I am developing an asp.net mvc application. I am creating robots.txt for my application to prevent from bots because my current site is getting many robot requests. So I found this link, Robots.txt file in MVC.NET 4 to create robots.txt. But I when I access my application like this entering url, "www.domain.com/robots.txt", it is always returning 404 page.
This is my action method in HomeController
public ActionResult Robots()
{
Response.ContentType = "text/plain";
return View();
}
This is my robots view
#{
Layout = null;
}
User-agent:*
Disallow:/
I configured route for robots.txt like this in RouteConfig
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//routes.MapMvcAttributeRoutes();
routes.MapRoute(
"Robots.txt",
"robots.txt",
new { controller = "Home", action = "Robots" },
new string[] { "AyarDirectory.Web.Controllers" }
);
//other routes
}
But when I access this url, "www.domain.com/robots.txt", it is always returning 404 page. How can I add robots.txt correctly to my application?

Creating a route ending with a file extension is not allowed by default in ASP.NET MVC. To get around this security restriction, you need to add the following to the Web.config file:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<!-- ...Omitted -->
<system.webServer>
<!-- ...Omitted -->
<handlers>
<!-- ...Omitted -->
<add name="RobotsText"
path="robots.txt"
verb="GET"
type="System.Web.Handlers.TransferRequestHandler"
preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>
</configuration>

In Asp.net Core you can simply add your robots.txt file to the wwwroot directory.

Would say same thing as above you need to change the web.config to allow the route with .txt to work. I had same issue with a project I was working on and I got it to work.
However if you using a view for the output of the robots without a model you might as well keep a static robots.txt as it will give you no advantage. Another way is to output the text direct from the action using a string builder.

Nkosi's System.Web.Handlers.TransferRequestHandler approach in web.config approach did not work for me due to the environment I was working in, resulting in 500 errors.
An alternative of using a web.config url rewrite rule worked for me instead:
<rewrite>
<rules>
<rule name="Dynamic robots.txt" stopProcessing="true">
<match url="robots.txt" />
<action type="Rewrite" url="/DynamicFiles/RobotsTxt" />
</rule>
</rules>
</rewrite>

Related

sitemap.xml url returns 404 Error

I have a large affiliate marketing site with millions of products hosted on Windows Azure.
For SEO I have to provide a sitemap.xml which is dynamically created.
public ActionResult SiteMap()
{
string sitemapUrl = "https://trendley.blob.core.windows.net/sitemap/sitemap.xml";
byte[] bImage = null;
using (WebClient wc = new WebClient())
{
bImage = wc.DownloadData(sitemapUrl);
}
return File(bImage, "application/octet-stream");
}
I added the follwoing route to my RouteConfig:
routes.MapRoute("Sitemap",
"sitemap.xml",
new { controller = "Home", action = "Sitemap" });
Unfortunately this is not workting.
I get -> HTTP Error 404.0 - Not Found
When I change "sitemap.xml" to sitemapxml (remove the extension) my controller method is invoked.
Already did some research and played with my web.config but nothing seems to work.
First thing I tried was to add:
<modules runAllManagedModulesForAllRequests="true" />
Second thing:
<add
name="AdfsMetadata"
path="sitemap.xml"
verb="GET"
type="System.Web.Handlers.TransferRequestHandler"
preCondition="integratedMode,runtimeVersionv4.0" />
Can someone tell me how to acomplish this.
Do I have to write my own Handler for this?
Cheers,
Stefan
The reason that that route is not working is because by default .xml is handled by the "StaticFileHandler" in IIS so when the request comes in ASP.net is not invoked.
Option 1: Enable runAllManagedModulesForAllRequests - in your web .config add the following
<modules runAllManagedModulesForAllRequests="true" />
It goes inside of the system.webserver node.
option 2: Add a mapping for .xml to IIS and force that file extension into the ASP.net pipeline. See here
Please follow these steps:
1- Delete sitemap.xml from root of website directory (if exists)
2- Put MapRoute for sitemap.xml over other MapRoutes like this :
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Sitemap",
"sitemap.xml",
new { controller = "Home", action = "Sitemap" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
The reason of error is when sitemap RouteMap is under other rules , MVC checks the /sitemap.xml with above RouteMap , then throws error 404 for no matching controller/action.
I Know this is an old topic, but I have a solution that is better than "runAllManagedModulesForAllRequests".
Modules Preconditions:
The IIS core engine uses preconditions to determine when to enable a particular module. Performance reasons, for example, might determine that you only want to execute managed modules for requests that also go to a managed handler. The precondition in the following example (precondition="managedHandler") only enables the forms authentication module for requests that are also handled by a managed handler, such as requests to .aspx or .asmx files:
<add name="FormsAuthentication" type="System.Web.Security.FormsAuthenticationModule" preCondition="managedHandler" />
If you remove the attribute precondition="managedHandler", Forms Authentication also applies to content that is not served by managed handlers, such as .html, .jpg, .doc, but also for classic ASP (.asp) or PHP (.php) extensions. See "How to Take Advantage of IIS Integrated Pipeline" for an example of enabling ASP.NET modules to run for all content.
You can also use a shortcut to enable all managed (ASP.NET) modules to run for all requests in your application, regardless of the "managedHandler" precondition.
To enable all managed modules to run for all requests without configuring each module entry to remove the "managedHandler" precondition, use the runAllManagedModulesForAllRequests property in the <modules> section:
<modules runAllManagedModulesForAllRequests="true" />
When you use this property, the "managedHandler" precondition has no effect and all managed modules run for all requests.
You can learn more from it's original topic: runAllManagedModulesForAllRequests=“true” Meaning
So, the better way to set a HTTP handler for a .XML url on MVC is the following:
<system.webServer>
<handlers>
<add name="Sitemap" path="sitemap.xml" type="System.Web.UI.PageHandlerFactory" verb="*" />
</handlers>
</system.webServer>

MVC routing for both .mvc and extensionless URLs

I have an odd question. I'm working on some MVC code that was setup to run extensionless, as in /Home/Index?id=10 However, it used to be setup for IIS 6, and was using the .mvc extension on all the controllers, as in /Home.mvc/Index?id=10.
I would like both routes to be able to work. In my global.asax.cs file, I created the following code:
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute("Default2",
"{controller}.mvc/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute("Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
This almost worked! But unfortunately when my controllers return RedirectToAction, the URL which is generated fails.
I call RedirectToAction like this:
return RedirectToAction("Index", "Account");
and it returns this:
/Account.mvc
Now Index is assumed so I'm not worried that that's missing. However, there is no / on the end of that URL, and because of that, it is returning a 404 error. If I add the / it works fine. Is there some way I can help MVC to generate the URLs correctly, or is there a way I can modify the routes to make it recognize this URL?
Background: I'm in this situation because I began converting my application to use extensionless URLs after we upgraded our webserver to IIS 7.5. I didn't realize that there were alot of links into the applications which had been shared with the outside world--changing to extensionless broke all those links. Doh! Should've left well enough alone.
I would personally use the URL Rewriter. You can Redirect ASP.NET Legacy URLs to Extensionless with the IIS Rewrite Module.
So for example you could create an outbound rules that looked something like (not exactly, they definitely need testing):
<outboundRules>
<rule name="remove .mvc outbound 1">
<match serverVariable="RESPONSE_LOCATION"
pattern="^/(.*).mvc$" />
<action type="Rewrite" value="/{R:1}" />
</rule>
<rule name="remove .mvc outbound 2">
<match filterByTags="A, Form, IFrame, Img, Input, Link, Script"
pattern="^/(.*).mvc$" />
<action type="Rewrite" value="/{R:1}" />
</rule>
</outboundRules>
If someone decided to hardcode an MVC extension...
<rule name="Add Slash Inbound">
<match url="(.*)" />
<conditions>
<add input="{PATH_INFO}" pattern="^/(.*).mvc$" negate="true" />
</conditions>
<action type="Rewrite" url="{R:0}/" />
</rule>
Looks like here is coded your solution with adding / at end of each requests Add a trailing slash at the end of each url?. If it is not enough We can try something else :)
For anyone trying to figure this out: the problem may not be that RedirectToAction is generating a bad URL, but actually that the URL is not being interpreted properly by IIS.
I discovered that other servers had no problem with the missing slash in the URLs described in the original question.
This led me to look at the App Pool setting. One setting in particular seems to affect this: "Enabled 32-bit applications". Setting this to true on the Application Pool enabled my application to parse URLs which were missing the trailing '/'.
Weird huh?

How to add route to dynamic robots.txt in ASP.NET MVC?

I have a robots.txt that is not static but generated dynamically. My problem is creating a route from root/robots.txt to my controller action.
This works:
routes.MapRoute(
name: "Robots",
url: "robots",
defaults: new { controller = "Home", action = "Robots" });
This doesn't work:
routes.MapRoute(
name: "Robots",
url: "robots.txt", /* this is the only thing I've changed */
defaults: new { controller = "Home", action = "Robots" });
The ".txt" causes ASP to barf apparently
You need to add the following to your web.config file to allow the route with a file extension to execute.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<!-- ...Omitted -->
<system.webServer>
<!-- ...Omitted -->
<handlers>
<!-- ...Omitted -->
<add name="RobotsText"
path="robots.txt"
verb="GET"
type="System.Web.Handlers.TransferRequestHandler"
preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>
</configuration>
See my blog post on Dynamically Generating Robots.txt Using ASP.NET MVC for more details.
Answer here: url with extension not getting handled by routing. Basically, when asp sees the "." it calls the static file handler, so the dynamic route is never used. The web.config files needs to be modified so /robots.txt will not be intercepted by the static file handler.
Muhammad Rehan Saeed's System.Web.Handlers.TransferRequestHandler approach in web.config approach did not work for me due to the environment I was working in, resulting in 500 errors.
An alternative of using a web.config url rewrite rule worked for me instead:
<rewrite>
<rules>
<rule name="Dynamic robots.txt" stopProcessing="true">
<match url="robots.txt" />
<action type="Rewrite" url="/DynamicFiles/RobotsTxt" />
</rule>
</rules>
</rewrite>

ASP.NET MVC 4 - 301 Redirects in RouteConfig.cs

How can I add a route to the RouteConfig.cs file in an ASP.NET MVC 4 app to perform a permanent 301 redirect to another route?
I would like certain different routes to point at the same controller action - it seems a 301 would be best practice for this, specially for SEO?
Thanks.
You have to use RedirectPermanent, here's an example:
public class RedirectController : Controller
{
public ActionResult News()
{
// your code
return RedirectPermanent("/News");
}
}
in the global asax:
routes.MapRoute(
name: "News old route",
url: "web/news/Default.aspx",
defaults: new { controller = "Redirect", action = "News" }
);
I know you specifically asked how to do this on the RouteConfig, but you can also accomplish the same using IIS Rewrite Rules. The rules live on your web.config so you don't even need to use IIS to create the rules, you can simply add them to the web.config and will move with the app through all your environments (Dev, Staging, Prod, etc) and keep your RouteConfig clean. It does require the IIS Module to be installed on IIS 7, but I believe it comes pre installed on 7.5+.
Here's an example:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect t and c" stopProcessing="true">
<match url="^terms_conditions$" />
<action type="Redirect" url="/TermsAndConditions" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

ApiController returns 404 when ID contains period

I have an ApiController and I want to use email addresses as the ID parameter for requests:
// GET api/employees/email#address.com
public CompactEmployee Get(string id) {
var email = id;
return GetEmployeeByEmail(email);
}
However, I cannot get this to work (returns 404):
http://localhost:1080/api/employees/employee#company.com
The following all work:
http://localhost:1080/api/employees/employee#company
http://localhost:1080/api/employees/employee#company.
http://localhost:1080/api/employees?id=employee#company.com
I have set relaxedUrlToFileSystemMapping="true" in my web.config as detailed by Phil Haack.
I would very much love the full email address to work, but any time the period is followed by any other character, the request returns a 404. Any help would be greatly appreciated!
Solution
Due to a lack of other options, I've headed in the direction Maggie suggested and used the answer from this question to create a rewrite rule to automatically append a trailing slash when I need an email in the URL.
<system.webServer>
....
<rewrite>
<rules>
<rule name="Add trailing slash" stopProcessing="true">
<match url="^(api/employees/.*\.[a-z]{2,4})$" />
<action type="Rewrite" url="{R:1}/" />
</rule>
</rules>
</rewrite>
</system.webServer>
Would adding a trailing slash work for your scenario?
http://localhost:33021/api/employees/employee#company.com/
Check your IIS settings:
Home Directory -> Configuration
Edit the .aspx application extension and ensure that the setting Verify that file exists is off.
UPDATE
I've just tested with a default MVC4 Web API project
URL: http://localhost:10983/api/values/cool#email.com
Action in ValuesController:
public string Get(string id)
{
return id;
}
This was the response:
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">cool#email.com</string>
This is what worked for me:
I was running on targetFramework = 4.6.1. I have upgraded to 4.6.2 and added this in web.config:
<system.web>
<customErrors mode="Off"/>
<compilation debug="true" targetFramework="4.6.2"/>
<!-- This will allow to search for stuff that contains . & etc.-->
<httpRuntime targetFramework="4.6.2" maxRequestLength="100000" maxUrlLength="2048" relaxedUrlToFileSystemMapping="true" requestPathInvalidCharacters=""/>
</system.web>
The requestPathInvalidCharacters="" is to be able to have stuff like & etc in URI, in encoded form, of course.

Resources