Caching versioned static files with ASP.NET MVC on IIS7 - asp.net-mvc-2

<system.webServer>
...
</handlers>
<staticContent>
<clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="365.00:00:00" />
</staticContent>
<rewrite>
<rules>
<rule name="StaticFiles" stopProcessing="true">
<match url="StaticContent/[\d.]+/(.*)$" />
<action type="Rewrite" url="StaticContent/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
I am trying to add Caching versioned static files with ASP.NET MVC on IIS7.
I got "The element 'system.webServer' has invalid child element 'rewrite'" error.
How can I fix this?

There is this post over at asp.net that may be of help. Als there is this scottgu post. I hope those will get you on the right path.

Related

Redirect URL but not subdirectories IIS 8

Currently, I have IIS 8 set up to answer www.IPO.is with a static website. I then have asp.net projects answering to specific sub-directories of www.IPO.is (e.g. www.IPO.is/test).
Now I'm moving the static website to a different location and URL so I want the old URL to redirect to the new project. (ipo.keldan.is)
Is there any way to have IIS answer www.ipo.is with a redirect but still serve www.ipo.is/test as before?
You can use IIS UrlRewrite extension. Create a web.config file at the root of your static website directory and then paste in the following:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect rule" patternSyntax="ECMAScript" stopProcessing="true">
<match url="/$" />
<action type="Redirect" url="http://ipo.keldan.is" />
<conditions>
<add input="{HTTP_HOST}" pattern="^ipo.is$" />
</conditions>
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Ensure you have UrlRewrite extension installed before applying this update. You can install UrlRewrite using Microsoft WebPlatform installer
After a good hint from Mohsin Mehmood I managed to find a solution for this problem.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="IPO Redirect" stopProcessing="true">
<match url="(.+)" negate="true" />
<action type="Redirect" url="http://ipo.keldan.is" redirectType="Permanent" />
<conditions>
<add input="{HTTP_HOST}" pattern="(.*)ipo.is" />
</conditions>
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
This matches any URL that does not have any sub-directories for the host ipo.is (with any variants of http/https and www/non-www.

In IIS can I change the default document per folder via web.config?

I have a certain PHP file on my windows server in a directory that requires you to add index.php in order to view it.
The below works:
http://example.org/placestorun/index
But the following does not work:
http://example.org/placestorun/
I added a web.config file to the places to run directory to make it work without the index.php using the below code in the folder's web.config file:
<configuration>
<system.webServer>
<defaultDocument>
<files>
<add value="index.php" />
</files>
</defaultDocument>
</system.webServer>
</configuration>
The above is not working though.
I ended up just doing a rewrite in web.config:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Imported Rule placestoswim" stopProcessing="true">
<match url="^$" ignoreCase="false" />
<action type="Rewrite" url="index.php" appendQueryString="false" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
You need to add clear to remove all the inherited elements:
<configuration>
<system.webServer>
<defaultDocument enabled="true">
<files>
<clear />
<add value="index.php" />
</files>
</defaultDocument>
</system.webServer>
</configuration>
Also make sure your app is not running in classic mode but in integrated mode.

Azure webapp Web.config httpErrors not redirecting

I have an Azure-hosted website that is working great. The thing I'm stuck on is a desire to ignore anything after the base URL, and always have the user see the single page that is the whole site. (If they type the http://example.com site, anything after that will be ignored, and my map.html page will be shown.
I have made the following changes to the Web.config file, which help this along:
<system.webServer>
<defaultDocument enabled="true">
<files>
<clear />
<add value="map.html" />
</files>
</defaultDocument>
<httpErrors errorMode="Custom" defaultResponseMode="ExecuteURL">
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404 path="/map.html" responseMode="ExecuteURL" />
</httpErrors>
</system.webServer>
If I enter a url of example.com/xxxx.yyyy the site displays fine (it ignores the "/xxxx.yyyy" and shows the map.html page - just what I want. But if I enter example.com/xxxx without the trailing ".yyyy") the following IIS or Azure message shows:
Server Error in '/' Application.
The resource cannot be found.
Description: HTTP 404 The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporally unavailable...
Requested URL: /xxxx
How can I get the same redirect to happen no matter what follows the site name in the url?
I've tried Windows Edge, Chrome, and Safari, and they all give the same results.
To achieve your requirement, you can add a rewrite rule instead:
<system.webServer>
<rewrite>
<rules>
<rule name="Rewrite to map">
<action type="Rewrite" url="/map.html"/>
</rule>
</rules>
</rewrite>
Update:
If you work in ASP.NET, you may also need to specify <customErrors> Element in Web.config:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.web>
<customErrors mode="On">
<error statusCode="404" redirect="~/map.html" />
</customErrors>
</system.web>
<system.webServer>
<defaultDocument enabled="true">
<files>
<clear />
<add value="map.html" />
</files>
</defaultDocument>
<httpErrors errorMode="Custom" defaultResponseMode="ExecuteURL">
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404" path="/map.html" responseMode="ExecuteURL" />
</httpErrors>
</system.webServer>
</configuration>

Zend Framework Rewrite does not work for some URLs under IIS7

I recently moved a web site written based on Zend framework 1 from Apache to IIS7. I followed the instructions by Rob Allen http://akrabat.com/winphp-challenge/zend-framework-url-rewriting-in-iis7/ to import the rules into IIS.
Everything works fine except URL with this format:
www.example.com/products/download/productname-4.0.0.zip
It shows the IIS 404 error page, not the customized 404 page in the application. I suspect that it is IIS7 configuration issue.
Anyone has the similar experience here?
Here's a web.config that I use in IIS7 for Zend Framework
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Imported Rule 1" stopProcessing="true">
<match url="^.*$" />
<conditions logicalGrouping="MatchAny">
<add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" />
</conditions>
<action type="None" />
</rule>
<rule name="Imported Rule 2" stopProcessing="true">
<match url="^.*$" />
<action type="Rewrite" url="index.php" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

Can't import PMD Ruleset in Eclipse

I would like to use the same Ruleset in my IDE (Eclipse) that my Sonar profile.
I got the PMD XML ruleset from the Sonar Permalinks and would like to import it into my PMD Eclipse Plugin but when i try to do it, the "OK" button is desactivated ...
Can someone help me ?
The problem could be that Sonar is exporting your ruleset for v4.x format and your Eclipse plugin expects them in v5.x format.
Try changing your rules from:
<rule ref="rulesets/basic.xml/UnusedNullCheckInEquals">
<priority>3</priority>
</rule>
to
<rule ref="rulesets/java/basic.xml/UnusedNullCheckInEquals">
<priority>3</priority>
</rule>
Please note the ref attribute. A simple find and replace all will work out fine for you.
The advice from Ivan Nikolov helped me, but I had to also change one rule from:
<rule ref="rulesets/java/controversial.xml/UnusedModifier">
<priority>5</priority>
</rule>
to
<rule ref="rulesets/java/unusedcode.xml/UnusedModifier">
<priority>5</priority>
</rule>
Here is my PMD configuration file from sonar which is working for me with PMD-plugin 4.0.2.:
<?xml version="1.0" encoding="UTF-8"?>
<ruleset xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
name="pmd-eclipse"
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd">
<rule ref="rulesets/java/basic.xml/UnusedNullCheckInEquals">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/strings.xml/StringInstantiation">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/design.xml/ConstructorCallsOverridableMethod">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/strictexception.xml/AvoidCatchingNPE">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/strictexception.xml/AvoidRethrowingException">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/migrating.xml/ReplaceEnumerationWithIterator">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/optimizations.xml/AvoidArrayLoops">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/unusedcode.xml/UnusedFormalParameter">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/basic.xml/ClassCastExceptionWithToArray">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/strictexception.xml/AvoidThrowingNullPointerException">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/unusedcode.xml/UnusedPrivateField">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/design.xml/CompareObjectsWithEquals">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/strings.xml/UseIndexOfChar">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/basic.xml/BigIntegerInstantiation">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/design.xml/FinalFieldCouldBeStatic">
<priority>4</priority>
</rule>
<rule ref="rulesets/java/naming.xml/SuspiciousEqualsMethodName">
<priority>2</priority>
</rule>
<rule ref="rulesets/java/design.xml/InstantiationToGetClass">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/naming.xml/SuspiciousHashcodeMethodName">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/coupling.xml/LooseCoupling">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/unusedcode.xml/UnusedLocalVariable">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/strings.xml/UnnecessaryCaseChange">
<priority>4</priority>
</rule>
<rule ref="rulesets/java/design.xml/SingularField">
<priority>4</priority>
</rule>
<rule ref="rulesets/java/design.xml/CloseResource">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/strictexception.xml/AvoidCatchingThrowable">
<priority>2</priority>
</rule>
<rule ref="rulesets/java/basic.xml/CollapsibleIfStatements">
<priority>4</priority>
</rule>
<rule ref="rulesets/java/basic.xml/UselessOperationOnImmutable">
<priority>2</priority>
</rule>
<rule ref="rulesets/java/clone.xml/CloneMethodMustImplementCloneable">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/basic.xml/UselessOverridingMethod">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/unusedcode.xml/UnusedModifier">
<priority>5</priority>
</rule>
<rule ref="rulesets/java/design.xml/PreserveStackTrace">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/optimizations.xml/UseArraysAsList">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/design.xml/EqualsNull">
<priority>2</priority>
</rule>
<rule ref="rulesets/java/basic.xml/BrokenNullCheck">
<priority>2</priority>
</rule>
<rule ref="rulesets/java/logging-jakarta-commons.xml/UseCorrectExceptionLogging">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/strings.xml/InefficientStringBuffering">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/sunsecure.xml/ArrayIsStoredDirectly">
<priority>2</priority>
</rule>
<rule ref="rulesets/java/finalizers.xml/FinalizeOverloaded">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/controversial.xml/DontImportSun">
<priority>4</priority>
</rule>
<rule ref="rulesets/java/imports.xml/DontImportJavaLang">
<priority>4</priority>
</rule>
<rule ref="rulesets/java/design.xml/MissingStaticMethodInNonInstantiatableClass">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/strings.xml/StringBufferInstantiationWithChar">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/optimizations.xml/UseArrayListInsteadOfVector">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/strings.xml/StringToString">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/design.xml/SimplifyConditional">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/migrating.xml/ReplaceVectorWithList">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/codesize.xml/NcssMethodCount">
<priority>3</priority>
<properties>
<property name="minimum" value="50" />
</properties>
</rule>
<rule ref="rulesets/java/logging-java.xml/AvoidPrintStackTrace">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/naming.xml/MethodWithSameNameAsEnclosingClass">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/naming.xml/SuspiciousConstantFieldName">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/codesize.xml/NcssTypeCount">
<priority>3</priority>
<properties>
<property name="minimum" value="800" />
</properties>
</rule>
<rule ref="rulesets/java/design.xml/AvoidInstanceofChecksInCatchClause">
<priority>4</priority>
</rule>
<rule ref="rulesets/java/migrating.xml/IntegerInstantiation">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/migrating.xml/AvoidAssertAsIdentifier">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/design.xml/UnnecessaryLocalBeforeReturn">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/finalizers.xml/AvoidCallingFinalize">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/basic.xml/BooleanInstantiation">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/basic.xml/UnconditionalIfStatement">
<priority>2</priority>
</rule>
<rule ref="rulesets/java/clone.xml/CloneThrowsCloneNotSupportedException">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/migrating.xml/ReplaceHashtableWithMap">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/basic.xml/AvoidDecimalLiteralsInBigDecimalConstructor">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/strictexception.xml/SignatureDeclareThrowsException">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/finalizers.xml/EmptyFinalizer">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/design.xml/IdempotentOperations">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/strictexception.xml/ExceptionAsFlowControl">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/finalizers.xml/FinalizeDoesNotCallSuperFinalize">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/strings.xml/UseStringBufferLength">
<priority>4</priority>
</rule>
<rule ref="rulesets/java/migrating.xml/AvoidEnumAsIdentifier">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/logging-java.xml/SystemPrintln">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/strings.xml/UselessStringValueOf">
<priority>4</priority>
</rule>
<rule ref="rulesets/java/strings.xml/AvoidDuplicateLiterals">
<priority>3</priority>
</rule>
</ruleset>
I had to add the language to my XPath rule(s) as well, e.g:
<rule name="DontUseDate" language="java" class="net.sourceforge.pmd.lang.rule.XPathRule" ...></rule>
In addition to what others said, you may also have to find this:
net.sourceforge.pmd.rules.XPathRule
and replace it by this:
net.sourceforge.pmd.lang.rule.XPathRule
Ivan Nikolov's answer is correct. Your problem are different versions of ruleset format . In my case I was trying to import ruleset exported from PMD plugin version 3.2.6 into plugin version 4.0.0. Here are my findings:
Path change that Ivan Nikolov mentioned is needed for all the rules. Be careful because not all the rules belong to java (I saw also rules for XML,...). I am not that deep into PMD, so I don't know in which version were rules for other technologies introduced.
It is useful to export default ruleset of PMD plugin 4.0.0 for comparison with old custom ruleset file. This way you can fix problems that can be spotted at next point.
During your behavior was thrown exception that wasn't visualized. This exception can be seen in your workspace log file (/.metadata/.log) at the end. This can give you a clue what else needs to be changed. Use the previous point to find out how to fix problems. In my case it was changed path of one rule from
to
After these changes I was able to import manually migrated custom ruleset file into PMD plugin 4.0.0.
Hope this helps.
BTW: I find 4.0.0 version of PMD plugin very buggy so I downgraded back to 3.2.6
Why don't you use Sonar Eclipse ? You wouldn't need to worry about synchronization of rule sets.