How to hide extension of my asp.net application - asp.net

I have created a website in asp.net application. Which contains hundreds of pages for Insert Update Edit and Delete.
I need URL rewriting without doing too much of code. Please guide me.
My Application URL like.
http://domain/Users/index.aspx http://domain/Users/product.aspx
http://domain/Users/product.aspx?catid=1
http://domain/Users/product.aspx?typeid=1
http://domain/Users/product.aspx?editid=1&type=2
I want to hide the .aspx from whole application and also the URL doesn't show the .aspx page url if user open directly http://domain/Users/product.aspx?typeid=1.
Please let me know if any solution.

What you're looking for is FriendlyUrls which will enable you to achieve what you want. Firstly you will need to install the package which you can do by using NuGet Console
Install-Package Microsoft.AspNet.FriendlyUrls
Then in your RouteConfig Class you want something like below which should do the trick.
public static void RegisterRoutes(RouteCollection routes)
{
routes.EnableFriendlyUrls();
}
There's a handy article here which I reccomend you read.

Related

Routing for Single Page Application in ASP.NET Core

I have a Single Page Application written in JavaScript, and I use HTML5 history API to handle URLs on the client side. This means any URLs sent to the server should cause the server to render the same page.
In the ASP.NET MVC 5 I wrote this code to do this:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
// ...
routes.Add(new Route("{*path}", new MyRouteHandler()));
}
}
public class MyRouteHandler : IRouteHandler
{
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
return WebPageHttpHandler.CreateFromVirtualPath("~/index.cshtml");
}
}
This worked really well. No matter what URL the server gets, it renders index.cshtml. Note that I am able to use a .cshtml file (as opposed to an .html file) which means I can have some C# code to dynamically change what .js scripts are included, append version numbers to .css files, and so on. What's more, I didn't have to implement controllers and views and so on just to render this .cshtml file.
Now we come to the question: How do you do this in ASP.NET Core? I have been reading the documentation, but I don't see how to render a .cshtml file without adding controller classes, view folders and other rigmarole.
Anyone know the equivalent code in ASP.NET Core?
Currently to run a CSHTML page "the normal way" in ASP.NET Core requires using ASP.NET Core MVC.
However, there is a planned feature that is being worked on that is somewhat similar to ASP.NET (non-Core) Web Pages, where you can have standalone CSHTML files. That feature is being tracked here: https://github.com/aspnet/Mvc/issues/494 (and as far as naming for the new feature, that is being tracked here: https://github.com/aspnet/Mvc/issues/5208).
There's also a sample of how to render an MVC view to a string (e.g. to generate an email, report, etc.), and that sample is available here: https://github.com/aspnet/Entropy/tree/dev/samples/Mvc.RenderViewToString
But to use this sample in the scenario you describe, you'd have to do some extra plumbing to wire it up as its own middleware (not a lot of plumbing; just a little!).
It's also worth noting that in your scenario you probably don't want all URLs going to this one view, because you still need the static files middleware running first to handle the CSS, JS, images, and other static content. Presumably you just want all other URLs to go to this dynamic view.

Keep URL extensions

I know that removing URL extensions is the new model for website programming. Unfortunately, my site is hosted on a hybrid server configuration. The call to my site goes into an Apache server that recognizes that my call is for a .aspx page, and passes the call along to an IIS server to complete the call. This complicates my website at this point because I am coding in Visual Studio 2015, and it models after the new rules of removing the extensions, and the call is never passed along to the IIS server.
I am not a big HTML guy, and I cannot find anything to place in web.config or my global.asax file for code to tell the system to overwrite the rule of removing the extension, and to keep my extensions. I have seen several posts here to remove the extensions, but nothing to keep them.
Basically, when I call www.mysite.com/Default.aspx, the current config removes the .aspx extension, and the call is for www.mysite.com/Default. I want to KEEP the .aspx extension on the call to the site so that it passes through the Apache server and to the IIS server. Any help would be greatly appreciated.
Have a look in your App_Start directory for a class called RouteConfig.cs and disable AutoRedirectMode using this line of code
settings.AutoRedirectMode = RedirectMode.Off;
This is what automatically removes extensions from your web pages.
Full example below...
public static class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
var settings = new FriendlyUrlSettings();
settings.AutoRedirectMode = RedirectMode.Off;
routes.EnableFriendlyUrls(settings);
}
}

Use Razor views as email templates inside a Web Forms app

I have an asp.net Web Forms application that needs to send emails. I like the Razor syntax and as you can use Razor outside of MVC I thought I'd try and use that. I've seen that you can programmatically pass a template string to razor but I'd like to keep my razor templates as separate .cshtml files.
Does anyone have any simple, idiot-proof advice as to how to do this? I've tried to load them from file like this:
UserDetails userDetails = new UserDetails {Name = "Fred"};
string template = File.OpenText("Email/UserDetailsEmail.cshtml").ReadToEnd();
string messageText = Razor.Parse(template, userDetails);
All to no avail. There's an exception finding the file. I'm using the razorengine dll.
I have everything else working, the smtp server etc, just not the views.
Any help appreciated.
You need to get the full path to the file; relative paths will end up being relative to the wrong location.
Write
File.OpenText(Server.MapPath("~/Email/UserDetailsEmail.cshtml"))
I would use the MVCMailer tool which you can setup with Nuget and has scaffolding for creating the views.
It is a very well written app and can be added to yours easily:
https://github.com/smsohan/MvcMailer
http://www.codeproject.com/KB/aspnet/MvcMailerNuGet.aspx

Creating Application/Virtual Directories in a site that POINT to that site to simulate multiple sites. Good idea?

We need a way to say
www.site.com/india
www.site.com/asia
www.site.com/usa
etc...
But we want all these requests to point back to www.site.com and not have to physically create the files and directories for every type of site... so if I just create a virtual directory (www.site.com/india) and point it to www.site.com... then I figure I can look at the URL and set some parameters/text/images accordingly to fill out the template...
Does this make sense? Or are there issues with web.configs ? OR a better way
I would encourage you to consider Routing, as demonstrated here: http://www.asp.net/mvc/tutorials/asp-net-mvc-routing-overview-cs
The given sample, repeated here so you can (hopefully) see the relevance, is:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace MvcApplication1
{
// Note: For instructions on enabling IIS6 or IIS7 classic mode,
// visit http://go.microsoft.com/?LinkId=9394801
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
}
protected void Application_Start()
{
RegisterRoutes(RouteTable.Routes);
}
}
}
By similar methods, you can merely extract the information you need
The first example was for MVC Routing, to do webforms routing you should look here http://msdn.microsoft.com/en-us/library/cc668202(VS.90).aspx and here's a (very limited) example:
public static void RegisterRoutes(RouteCollection routes)
{
routes.Add("BikeSaleRoute", new Route
(
"bikes/sale",
new CustomRouteHandler("~/Contoso/Products/Details.aspx")
));
}
Using a custom routehandler that they build in the code. Routehandlers are easy to use, once you get the hang of it. And they allow you more expressiveness.
I think you'll find that localizing the same site five ways without having different web.configs pointing you to your localization files will be difficult.
What I would do is have www.site.com's index wither automatically discover the client's location or ask them what version of the site they want. Then, refer them to a default.aspx under that site's virtual directory, that uses a common set of your custom web controls, localized according to the resource file referenced in that site's web.config. That way, all you'll need to copy is the site skeleton, which can be as little as the index.aspx.
If the site is mostly aspxs, this isn't going to work as well. In that case, I would do the website selection as before, but use a query string to keep track of client location and thus localization. That should still allow you to bring up the correct localization settings.
No, please promise me not to create a virtual directory for each country in the world. Checkout routing instead. And here are some other examples.
Consider using routing or even better MVC if you haven't written the sites already. You should definitely NOT be creating multiple virtual directories - it would be a nightmare to maintain

ASP.NET MVC - How to make it work with IIS6

I am having some issues with deploying my MVC 2 application on a IIS 6 server.
I have the following project structure:
/
App/
Controllers/
Helpers/
Infrastructure/
Models/
Views/
Public/ # This folder contains CSS and JS files
Global.asax
Web.config
I have a custom System.Web.Mvc.WebFormViewEngine that tells my application to lookup the views in /App/Views instead of the default /Views.
It works fine on Cassini and IIS 7.5.
I need to deploy my application in a virtual directory on IIS 6 and I am getting 404 errors when trying to access any of my controllers.
I read that I needed to add a Default.aspx with the following code behind:
protected void Page_Load( object sender, EventArgs e ) {
HttpContext.Current.RewritePath( Request.ApplicationPath, false );
IHttpHandler httpHandler = new MvcHttpHandler();
httpHandler.ProcessRequest( HttpContext.Current );
}
It actually called my default controller, and showed the corresponding view, but it's the only page I've been able to get so far.
I tried to enable the wildcard mapping, it didn't change anything. But I'm using ASP.NET 4.0, and it enables routing of extension-less URLs.
I'm not really sure what to do now, I'm not finding any other helpful sources of information on the Internet.
How could I make it work?
See this walkthrough by Phil Haack.
Can't comment yet, but that walkthrough is it.
I did wildcard myself.
It was a while ago, so I don't remember the damn details of what I had to do to get it fixed now, but it took me a few hours.
I was missing some really small detail in his instructions, if I remember correctly. What error/incorrect behavior are you getting? You might trigger my memory.

Resources