I need to disable a route temporarily in an asp.net webapi app and I cannot do compile and do another deploy.
Can I just a disable a asp.net web api route in web.config?
I don't think you can disable a specific route in web.config. Normally if you want to disable a route, you can use IgnoreRoute, or just remove the route in WebApiConfig.cs or Global.asax file wherever the route exists.
Since you only want to do it in web.config without doing compile and another deploy, I can only think about two ways.
1) Use URL Rewriting
<rewrite>
<rules>
<clear />
<rule name="diableRoute" stopProcessing="true">
<match url="api/YourWebApiController" />
<action type="Redirect" url="Error/NotFound" appendQueryString="false" />
</rule>
</rules>
</rewrite>
This result will return the 404 Error page.
2) Setting authorization rules
<location path="api/YourWebApiController">
<system.web>
<authorization>
<deny users="*"/>
</authorization>
</system.web>
</location>
This result will return to the login page, because the access to the web api route is denied.
You can customize either one to your need.
Related
The Seo Company ask for making redirect each 404 bad request to specific url.
I asked for redirect an error 400 (Bad request) to special page.
I can do it in the web.config file, but the requirement is each url to special page
for example
https://stackoverflow.com/questions/askhttps://stackoverflow.com/questions/ask =>(301) https://stackoverflow.com/questions/ask
https://stackoverflow.com/questionshttps://stackoverflow.com/questions => (301) https://stackoverflow.com/questions
Is it possible?
My application is ASP.NET (Not MVC) on IIS SERVER
Thanks
Caveat:
You say this is "ASP.NET (not MVC)". I'm going to assume this means you're using WebForms rather than ASP.NET Core. The following advice can be used in both cases, but in case of ASP.NET Core only if you're hosting with IIS.
Answer:
You're looking for IIS URL Rewrite, a module that can be installed in IIS then configured via your web.config. One feature this provides is rewrite maps which are essentially a list of from and to URL mappings where a request for the former will cause a redirect to the latter. The documentation for URL Rewrite maps is here, and here's an example from the documentation:
Define your mappings:
<rewrite>
<rewriteMaps>
<rewriteMap name="StaticRewrites" defaultValue="">
<add key="/article1" value="/article.aspx?id=1&title=some-title" />
<add key="/some-title" value="/article.aspx?id=1&title=some-title" />
<add key="/post/some-title.html" value="/article.aspx?id=1&title=some-title" />
</rewriteMap>
</rewriteMaps>
</rewrite>
Then add a rewrite rule to use the mappings you just defined:
<rules>
<rule name="Rewrite Rule">
<match url=".*" />
<conditions>
<add input="{StaticRewrites:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Rewrite" url="{C:1}" />
</rule>
</rules>
In your case you'll want to use the Redirect action rather than Rewrite.
The module provides a lot of other configuration options.
On asp.net webforms set a page i.e. Redirect.aspx to redirect all the Error of type 400:
<configuration>
<system.web>
<customErrors defaultRedirect="Error.htm"
mode="RemoteOnly">
<error statusCode="400"
redirect="Redirect.aspx"/>
</customErrors>
</system.web>
then on the page see the Url it cames from by:
Request.UrlReferrer
then handle accordingly
How can I do the following?
I want a user to browse to https://localhost/Registration/GetCaptchaAudioInternetExplorer.wav and have it run the action of GetCaptchaAudioInternetExplorer on the Registration controller, which serves an audio/wav file.
What works for me right now is browsing to https://localhost/Registration/GetCaptchaAudioInternetExplorer
But what do I need to do to make https://localhost/Registration/GetCaptchaAudioInternetExplorer.wav route to the same action? Is there a way in MVC to specify action routes for something like this?
You can use URL Rewrite for IIS (7+) to do this, basically:
<rule name="Rewrite Wav Files" stopProcessing="true">
<match url="^Registration/?(.*)\.wav$" />
<action type="Rewrite" url="/Registration/{R:1}" />
</rule>
That will strip off the extension and send it to the controller. It's rewritten so it should still present as Registration/GetCaptchaAudioInternetExplorer.wav in the browser.
Potentially you can try setting relaxedUrlToFileSystemMapping:
<system.web>
<httpRuntime relaxedUrlToFileSystemMapping="true" />
But with .wav being a real thing, I'm not sure if that will work. More detail on Haacked.
A final alternative you can enable RAMMFAR:
RAMMFAR means "runAllManagedModulesForAllRequests" and refers to this
optional setting in your web.config.
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
</system.webServer>
That should send all requests through MVC regardless of extension. I say final, as this has a performance hit.
A little bit of a spinoff, but I was able to instead get https://localhost/Registration/GetCaptchaAudioInternetExplorer/clip.wav routing to my GetCaptchaAudioInternetExplorer action.
Two simple changes:
1) Add this route to your action:
[Route("clip.wav")]
public async Task<ActionResult> GetCaptchaAudioInternetExplorer()
2) Add this handler into your web.config:
<system.webServer><handlers><add name="Wave File Handler" path="clip.wav" verb="GET" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
I need to use Url Rewriting, so I made a test case, in Web.config, to check if it's working:
Web.config:
<system.webServer>
<rewrite>
<rules>
<rule name="Fail bad requests">
<match url=".*"/>
<action type="AbortRequest" />
</rule>
</rules>
</rewrite>
... other stuff
</system.webServer>
I was expecting any localhost:3285 to abort and fail, but it entered correctly.
I'm using Url Rewrite with IIS Express.
I am guessing your problem is browser caching. I have found that if I open a web page in IE, then add URL rewrite rules (requires stopping IIS express), then open the same page again in IE it still loads the page (and it appears the abort rule is not being applied). But, if I clear the IE browser cache and refresh the page the abort occurs.
Here is a full Web.config example that works for me
<?xml version="1.0" encoding="utf-8"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
<system.webServer>
<rewrite>
<rules>
<rule name="Fail bad requests">
<match url=".*"/>
<action type="AbortRequest" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Your example should work because the URL rewrite module is built into the current version of IIS Express:
http://www.iis.net/learn/extensions/introduction-to-iis-express/iis-express-overview
I am creating a search page in ASP.NET MVC3.
The url of calling the action was:
http://mydomain/Search?q=searchterm
it works fine if i search the keyword "web.config":
http://mydomain/Search?q=web.config
But now, i want the url to be:
http://mydomain/Search/searchterm
I have done this with adding the route into global.asax, but when i search "web.config", like http://mydomain/Search/web.config the server will end my request, because it thinks i am requesting the physical web.config file in the search directory.
Is there anyway to let asp.net consider the {q} in the url "search/{q}" as the parameter of the search action, not a request of a file?
In your RegisterRoutes in Global.asax you could enable requests for existing files to pass through the routing engine:
routes.RouteExistingFiles = true;
Notice that if you do that all request will now go through the ASP.NET MVC routing engine. So if you don't want to see broken images or javascript and CSS references you will need to explicitly exclude them:
routes.IgnoreRoute("scripts/{resource}.js");
routes.IgnoreRoute("content/{resource}.css");
routes.IgnoreRoute("iamges/{resource}.png");
routes.IgnoreRoute("iamges/{resource}.jpeg");
...
Also if you are hosting your application in IIS 7+ you need to remove some of the security filters that prevent you from serving web.config an .config files in general:
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<modules runAllManagedModulesForAllRequests="true" />
<security>
<requestFiltering>
<fileExtensions>
<remove fileExtension=".config"/>
</fileExtensions>
<hiddenSegments>
<remove segment="web.config"/>
</hiddenSegments>
</requestFiltering>
</security>
</system.webServer>
You could use the URL Rewriter Module in IIS to do this for you; it would remove the necessary logic from Global.asax and allow you to use it across your website for simplifying the urls.
Rewriter Module
Adding a Rewriter Rule
Sample rule:
<rewrite>
<rules>
<rule name="Rewrite to search">
<match url="^search/([_0-9a-z-]+)" />
<action type="Rewrite" url="search.aspx?q={R:1}" />
</rule>
</rules>
</rewrite>
Im trying to figure out how to setup a 301 permanent redirect for a website that is placed on a Microsoft-IIS/7.0 type server.
So let's say I have domain www.A.com and I want to redirect this to www.B.com I could use something like the following in my web.config file:
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect to WWW" stopProcessing="true">
<match url="A.com" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www.B.com$" />
</conditions>
<action type="Redirect" url="http://www.B.com/{R:0}"
redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
When placing the web.config in the root directory, the server responds:
403 - Forbidden: Access is denied.
You do not have permission to view this directory or page using the credentials that you supplied.
Any suggestion why this 403 error is given?
Thanks in advance.
If you are getting a 403 error with a plain (default) web.config and totally vanilla Default.aspx, then there is a configuration problem with IIS. Most likely the app pool does not have rights to the base folder for the website. Sounds like you're in a hosted situation so contact the system administrator.
This should work and also be much simpler. You don't need URL rewriting, just HTTP redirect.
As for the 403, does the IIS application pool have read access to the folder at the root of your site?
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<httpRedirect enabled="true" destination="www.B.com" httpResponseStatus="Permanent" />
</system.webServer>
</configuration>