ASP.NET URL Rewriting in very easy way - asp.net

I want to know how to make SEF URL for web pages. After a little googling I have found many techniques, but all of them are so complicated. I do not need them. Is there any way to make SEF URL which is very easy to implement?

First download this reference .
Then import it to your web site.
under <configuration> node type this:
<configSections>
<section name="urlrewritingnet" requirePermission="false" type="UrlRewritingNet.Configuration.UrlRewriteSection, UrlRewritingNet.UrlRewriter"/>
</configSections>
The next step is typing the following code under <system.web> node:
the next one is putting the following line just before </configuration> tag:
<urlrewritingnet configSource="RewriteRules.config"/>
We are almost done!
Create a new .config file which matches the tag's configResource parameter. I preferred "RewriteRules.config".
Then it is time to create rewriting rules on RewriteRules.config file:
<urlrewritingnet rewriteOnlyVirtualUrls="true" contextItemsPrefix="QueryString" defaultPage="Default.aspx" xmlns="http://www.urlrewriting.net/schemas/config/2006/07">
<rewrites>
<add name="DetailPageRule" virtualUrl="^~/VirtualDetailPageName/(.*)/(.*).aspx" rewriteUrlParameter="ExcludeFromClientQueryString" destinationUrl="~/RealDetailPage.aspx?param1=$1&param2=$2" ignoreCase=" true"/>
</rewrites>
</urlrewritingnet>
If you have more or less parameters you can change the count of (.)'s. Here there are only two parameters (param1 and param2), so there are only two (.) strings.
Final Step is giving links in according to the rules you have created.
It is simple and fast. But I do not know if there are drawbacks or any security issues.

Related

Api is giving 404 when added + as part of URL (%2B)

I am using asp.net core 3.1 and receiving values from URL. Its working fine but when I add "+" sign as a part of URL, it gives 404.
Example : localhost:9000/api/get/%2B12/values
+ is a special character. It should ideally be should be URL encoded as %2B.
Turns out it's not really required though (like in the screenshot below) but I still recommend it. (See: URL encoding the space character: + or %20?)
Here's a working example controller:
[ApiController]
public class ExpController : Controller
{
[Route("/api/exp/{arg}/values")]
public IActionResult Test(int arg) =>
Ok($"The arg is: {arg}");
}
Note how the route parameter is a int. For more complex values (other than just 12, +12, -12; eg: 12+12) the route will need to be a string instead.
version above IIS7 will refuse to request the URL contains symbols such as '+' by default. The following modifications are required. You need add the following nodes in web.config:
<system.webServer>
<security>
<requestFiltering allowDoubleEscaping="true"/>
</security>
</system.webServer>
But now the .net core project does not have web.config to configure IIS options. You need to go to the location:
vs project location /.vs/config/applicationhost.config to add the above node.
Note that the .vs folder is a hidden folder and needs to be set visible.
Option 1 :
Mess with config to bypass request validation / allowDoubleEscaping (Asp.Net)
You need to be aware for certain risk/vulnabilirities described here:
https://stackoverflow.com/a/53621095/4798459
.netcore :
Since this issues is related to IIS, not your solution. You need to handle a web.config
Create a new web.config on the root of your project.
Right click, properties, set "Copy to Output Directory" to "Copy Always"
When you publish a .net core app, a "basic web.config" file is created. (For iis)
You need to copy the content of the "basic web.config".
You can find the auto-generated web.config file:
Where your app is already published (local server?)
You can also publish your api temporarly to a random path on your PC, see details here https://docs.devexpress.com/OfficeFileAPI/401445/dotnet-core-support/publish-net-core-application)
The web.config should like so, i added the tag with a a commentt
<configuration>
<location path="." inheritInChildApplications="false">
<system.webServer>
<!-- XML node <security> is added to allow allowDoubleEscaping and add support for + paremeter in a route. Risk:https://stackoverflow.com/a/53621095/4798459 -->
<security>
<requestFiltering allowDoubleEscaping="true"></requestFiltering>
</security>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath="dotnet" arguments="[.\SolutionName.Namespace.dll]" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess" />
</system.webServer>
</location>
</configuration>
Make sure that the step 2 is done before publishing your app otherwise it wont work.
Not tested with iisexpress
Option 2
Change pramater type in the api. Intead of being on the route, use a queryString instead
Option 3
Custom solution for request filtetring /routing, which i don't have any example, and seems a bit "over the top".
Option 4, to avoid:
Use an other solution for encoding / decoding special caracter (I have not tried)
https://stackoverflow.com/a/55637235/4798459

LessAssetHandler is not invoked for path inside area

I have a problem with LessAssetHandler which ships with BundleTransformer.Less library. I use LessAssetHandler in debug mode as a HttpHandler for transforming less files. Everything works fine except that the files inside ASP.NET areas are not handled by LessAssetHandler. For example file /Content/Style/page.less is handled by LessAssetHandler, byt for file /Areas/Admin/Content/Style/page.less this handler is not invoked. I have configured handler in the folling way:
<system.webServer>
<handlers>
<add name="LessAssetHandler" path="*.less" verb="GET" type="BundleTransformer.Less.HttpHandlers.LessAssetHandler, BundleTransformer.Less" resourceType="File" preCondition="" />
<handlers>
</system.webServer>
How can I force MVC to route less files from area through LessAssetHandler?
Registration of LessAssetHandler is correct. The exact same code is added to Web.config file during installation of NuGet package.
BundleTransformer.Less can work in ASP.NET MVC application with areas. It is possible, that you have incorrectly registered the bundle.
Bundle registration should look like the following:
var adminStylesBundle = new CustomStyleBundle("~/Bundles/AdminStyles");
adminStylesBundle.Include("~/Areas/Admin/Content/Style/page.less");
adminStylesBundle.Orderer = nullOrderer;
bundles.Add(adminStylesBundle);

web.config in subdirectory in mvc 4

This question seems to have been asked quite a bit but none actually solve the problem for me. I have an ASP.NET MVC4 application with an area. Inside this area I have a web.config with overridden appsettings. These settings don't seem to be picked up when attempting to read them in a controller within the area.
-MyApp
-Areas
-MyArea
-Controllers
-MyController
-Web.config
-Web.config
In MyApp\Web.config I have:
<appSettings>
<add key="DefaultDisplayMode" value="Configuration" />
</appSettings>
In MyApp\Areas\MyArea\Web.config I have:
<appSettings>
<add key="DefaultDisplayMode" value="Review" />
<add key="SubSetting" value="Testing" />
</appSettings>
In MyController if I query the app settings, I see:
DefaultDisplayMode: Configuration
I expect to see:
DefaultDisplayMode: Review
SubSetting: Testing
I've tried with both:
System.Configuration.ConfigurationManager.AppSettings["..."]
System.Web.Configuration.WebConfigurationManager.AppSettings["..."]
I even tried with the following (over the top) structure with no luck:
-MyApp
-Areas
-MyArea
-Controllers
-MyController
-Web.config
-Web.config
-Web.config
Does anybody know why my subdirectory appSettings aren't working?
IIS will load the web.config based on the URL, not based on the code that your routed request executes.
You can create an empty folder structure that mimics the required route and add the config file there. If that does not work, you can use <location> tag in the main configuration file - but the path in the location tag also has to match the URL and not the code.

Meleze.web asp.net mvc 3 razor minify and compress html

I have find meleze.web in nuget to remove extra white spaces in generated html result.
but when i set web.config new , and run nothing happend to result.
is there any correct sample or special configs ?
<configuration>
<system.web.webPages.razor>
<host factoryType="Meleze.Web.Razor.MinifyHtmlWebRazorHostFactory,Meleze.Web.Razor" />
</system.web.webPages.razor>
</configuration>
For a little bit of context, we're talking about the tool described here: http://cestdumeleze.net/blog/2011/minifying-the-html-with-asp-net-mvc-and-razor/
I used the following config:
<configuration>
...
<system.web.webPages.razor>
<host factoryType="Meleze.Web.Razor.MinifyHtmlWebRazorHostFactory, Meleze.Web, Version=1.0.0.1, Culture=neutral, PublicKeyToken=0a868b5321967eda" />
...
Maybe you have changed the /web.config instead of the /Views/web.config that config.
Contact the package owner if needed to get help ;-)

Special characters in web.config

I've create a custom configuration section in my web.config file for my 3.5 web application. The custom configuration contains special characters listed below.
<add Key="2" String="â€"/>
<add Key="3" String="148"/>
<add Key="4" String="!X"/>
<add Key="5" String="¡§"/>
<add Key="6" String="¡¦"/>
<add Key="7" String="¡¨"/>
<add Key="8" String="’" />
<add Key="9" String="–" />
I currently have the xml type of the web.config defined as below.
<?xml version="1.0"?>
This works fine in one development environment, but when I migrate the application to another environment I get an xml parsing error on the string for Key 2. When I replace all the string definitions for each config entry with standard alpha chars, the application works fine. Is there a way to enforce the XML to be read the same way? I have looked into the encoding attribute for the xml definition tag, just not positive what to set it to. Any guidance would be well appreciated. Thanks in advance.
I recommend setting the encoding to "utf-8". Make sure the file is also actually saved as utf-8.
Are you typing these into your web.config file manually? Can't you just HTML-encode your string values?

Resources