Is it possible to get the QueryString value without using ? or & in the url?
I would like to have it like this:
http://www.colors.com/Red
string id = Request.QueryString["?"];
Instead of following:
http://www.colors.com/?ColorID=Red
string id = Request.QueryString["ColorID"];
No. a query string is defined by the appearance of a ?.
The example you give would redirect the user to a directory.
If you want to still be able to access the value of color-id through Querystring, then you should look at Rewriting. This can be due to legacy code that you can't change or other forms of interacting with 3rd party code. The benefit or Rewriting is that the code that ends up being executed doesn't know how the url looked like before it was rewritten and it can keep working as if there were a Querystring parameter named ColorID.
In its simplest form you need to call the Rewrite method of HttpContext, which will spin up a new request internally that executes code that matches that url without the user noticing anything. One caveat of this can be, that your legacy code doesn't know how to render correct links in menus and stuff, so you would keep having urls like ?ColorID=Red where it should have been just Red.
In IIS 7 and up, there is a built in filter where you can write your rules and patterns so you don't need to write your own code that matches incoming requests and calls HttpContext.Rewrite. Read more about it here on MSDN.
Now, Routing is a whole other thing. Its a Asp.net feature and doesn't work on top of existing legacy code but needs to be used with it. Meaning that the executing code needs to know that the request was routed to it. This of course has many benefits and of you're writing a new system then i would definitively recommend using Routing over Rewriting. There is a good article here about the differences and some SO questions also cover the topic:
IIS URL Rewriting vs URL Routing
Url Rewriting vs. Routing
It sounds like you may want to implement an MVC website.
Take a look at this MSDN documentation for more information.
Related
In the previous version of ASP.NET MVC, URLs like the following were tolerated: https://www.trap.com//house.
Now, this generates a 404, which makes sense in a way. I like that by default ASP.NET Core MVC now is less tolerant of nonsense in general.
But I am left wondering how to go about the suggestion of implementing a middleware to address the issue. I found this example, but I don't want to redirect the user to the "right" or canonical URL - I want to tolerate the bad one.
In other words, I want the URL https://www.trap.com//house to be routed as though it were the URL https://www.trap.com/house. Or the URL https://www.trap.com///house/////4 to be routed as though it were the URL https://www.trap.com/house/4.
What is the best way to achieve this? URL rewriting? Special routes? Something else?
The best way of course is to fix the applications sending incorrect URLs. In light of that not likely happening, I would suggest adding some URL rewrite rules that handle these before the url is handed off to MVC.
I ended up using the rewrite middleware to accomplish this, although I will probably end up removing it...
var rewriteOptions = new RewriteOptions()
.AddRewrite("^///?(.*)$", "/$1", false)
.AddRewrite("^///?(.*)///?(.*)$", "/$1/$2", false); //etc, maybe you could make a regex that catches all these patterns
app.UseRewriter(rewriteOptions);
I'm building an API using WebAPI that will be accessed via AJAX calls. However, the API controller will need more than just one POST method. I understand I can specify {action} in my routing, but because I've seen that this is not recommended - am I using the right tool? So 2 questions:
Is Web API the best tool for this, or is there something else I should be using?
Why should I not use more than one POST method in a WebApiController? Is including {action} in my routing a good enough solution to this problem?
1. Is Web API the best tool for this, or is there something else I should be using?
I think WebAPI is a fine choice for you, regardless of whether you have one or many POST calls per controller.
2. Why should I not use more than one POST method in a WebApiController?
To remain RESTFul you'll want a controller per entity type. Without getting too deep into details, a POST against a specific type of entity should be the 'ADD entity' call, and why would you have more than one of those? Having said that, you don't have to be fully RESTFul... if your requirements suite a multi-POST model then go for it, you can always refactor later if necessary.
...Is including {action} in my routing a good enough solution to this problem?
Again, if your goal is to be RESTFul then this isn't a great practice. However, if you have needs that are best achieved using action routings then go for it. REST is not the only model.
I have some code used to determine if a user is logged in and I want to put this on every page in an ASP.NET website so that only logged in users can view it. The problem is that the site is split into multiple projects/solutions so maintaining the single piece of code might be hard.
I was thinking I could create a class that inherits for System.Web.UI.Page and overrides Page_Init, but that would require changing all pages so they inherit from new new class. Also I don't think this will work across projects.
So then I thought approaching the problem from a different side: using AOP. I have never used Aspects before but it looks like I could use PostSharp to write an Aspect that injects code before every Page_Init (or maybe Page_Load?). This might work as a quick solution but I might run into problems if I need a page to not perform the authentication check (available to everyone).
Just to clarify, I already have a login solution; I am just looking for a checking that login on each page.
Look into HttpModules. The asp.net framework is already programmed so that a module runs on every page request, you just have to write it and add it to web.config.
http://msdn.microsoft.com/en-us/library/zec9k340(v=vs.71).aspx
EDIT: Here's a better link that demonstrates handling the BeginRequest event
http://msdn.microsoft.com/en-us/library/ms227673(v=vs.85).aspx
As #jrummell mentioned, there's MembershipProvider which is a great option, but if you're creating custom login solition, check this link which has a pretty simple login implementation step by step
Since you seem to have your login solution handled and working, creating a class that overrides the page_init sounds like your best option. This can work across other projects by simply creating that class in a separate project that you can included in your other solution(s)... To be honest, that's the easiest way to span the logic across multiple projects.. This will also be easily maintained because you'd only have to update one location going forward.
If you are using MasterPages, you wouldn't have to hit all of the pages, you could just include it on specific MasterPage(s) and set all the pages you want authentication to use that MasterPage.
Windows Identity Foundation can solve this for you. See http://msdn.microsoft.com/en-us/security/aa570351 for details on WIF. No need to reinvent the wheel. If you had only one Web application, Forms authentication would suffice.
I’ve only just started using ASP.NET MVC, and I have a somewhat trivial question: it seems that each controller has an attached folder-like path, so that my site becomes mydomain.net/Home/something. Is it possible to somehow get rid of the /Home part, so that the Home controller becomes ‘default’ for my web site and it’s possible to just use mydomain.net/something instead?
Sure, just define a route like so:
routes.MapRoute("{action}/{id}", new {controller="Home", action="Index", id=""});
The only problem is what about requests for your other controllers? For example, is
/Product/Foo
A request for HomeController.Product("Foo") or ProductController.Foo()?
You might need to use constraints to make the distinction clear.
I did a blog post on a simple way to handle this. I created a route constraint that selects a controller that you want to use for the root of your site. Here is the blog post if you're interested.
I would like to build a web application on Castle MonoRail, I was wondering how can we use an action with extension and another action without extension? How can HTML helper generator url for us?
Ex:
http://mysite.com/Products/list
http://mysite.com/Products/abc.castle
The answer is Yes, you can, by using routing.
The exact method you need to use to get the routing working depends upon which version of Monorail you are using. However, adding routing can have some negative impact on performance unless you are careful.
It is also worth noting that you don't have to use the extension ".castle" or ".rails" in case this is what is distasteful to you.
You can try creating a routing role like
/<controller>/<action>.castle
and it would do the trick.
I don't see a need for this. Why would you want to access an action without using .rails or .castle?