ASP.Net MVC 5 Routing wildcard + querystring - asp.net

I am using this Route filter
[Route("search/{*segments}")]
this takes all the segments I am providing which could be many.
Here is an example
http://localhost:50877/search/c_50_showcases%5E-displays/a_brand-name:33113319_balt:1623762%7Cmooreco:1672386/a_total-number-of-shelves:33111115429_5:3138:lt/so_ts
Now I also need the query string with this route but I am unable to make it work.
http://localhost:50877/search/?query=HP%20DesignJet&items=HEW51645A|ELI75220
It gives me 403 error.
The Web server is configured to not list the contents of this directory
How can I make a route that can take wildcard and query string to handle the incoming request. I am bound to use search in Route.
Also I tried with this
http://localhost:50877/search/test?query=HP%20DesignJet&items=HEW51645A|ELI75220
It works but this effects the SEO.

Defining action and route this way:
[System.Web.Mvc.Route("search/{*segments}")]
public ActionResult Search(string segments, string query, string items)
allows to get wildcard (in segments variable) and also query string parameters (query and items)

Related

Spring mvc controller - how to do url restrictions

This is spring mvc controller /city/{cityName}
#RequestMapping(value = "/city/{cityName}")
public String getCity (#PathVariable("cityName") String cityName, Model uiModel) {
}
www.example.com/city/{cityName}
Here cityName is dynamically loaded from url, my website can't support some of cityName,
it supports only Bengaluru, Kochin, Hyderabad and Chennai, due to dynamic things
it supports other cities or whatever in the place of cityName, it gives error,
how to restrict cityName for only 4 cities mentioned above.
Is there any way in controller itself or we have to maintain table(hard coded hashtable)
Suggest which is the best way to do
If you just want to have the four cities hardcoded in your code, you can also specify them using regular expression in #RequestMapping.
#RequestMapping("/cities/{cityName:Bengaluru|Kochin|Hyderabad|Chennai}")
public String getCity(#PathVariable("cityName") String cityName) {
}
That way if you specify a city which does not match the allowed values your controller will automatically return HTTP 404.
Update: Fixed the sample, the regex should go in #RequestMapping, not #PathVariable, sorry for the mistake
You'll either need separate controller mappings or to look up the cityName somewhere. That doesn't have to be hard-coded; you could instead use a YAML or properties file to initialize a Set or look it up in a database.
We can do at controller level statically
#RequestMapping(value = "/city/{cityname:bengaluru|chennai|kochin|hyderabad}")
OR we can create tables, query and put it in hashtable at application start up time.

Web API Pagination and controller route - GetAll with pageNumber vs GetByID conflict

I'm creating an API that needs to have the the following ability: (Example only)
GetALLProducts(int pageNumber) - with paging (page size is static)
Then, I need the other route for Getting a Product by ID
GetProduct(int productID)
the problem here is this is the same signature, so the route that should work for getting a product by id is also the same route that gets all products but with a pagenumber. The route that gets executed is the GetProduct.
Example: www.url.com/api/Products/2 - Does this get the second page of all products? or gets product id number 2?
I thought about adding pageSize to the signature, but I want to make this a system static value.
What do you think is the best resolution and clean solution here? I saw this question: WebApi Multiple actions were found with GetAll() and GetByIds(int[] ids) and this could work as well. Thoughts?
Thanks!!
Pass pagenumber as a query string. You should separate call that will be routed accordingly - by convention /products?page=1 and /product/2, respectively. Your specified route will be able to distinguish the two. Oh and my preference is to have attributed routing as suggested in the thread.

How to write method having many parameters in REST webservice

I need to develop a web method that has many parameters. In REST, I understand a webservice has its own significance by attaching itself to particular entity and HttpVerb determines operation type.
This webmethod cannot be associated with an entity, it just calls a stored procedure and returns data, so I assume it only has only a GET method. But it has too many parameters to be fit into a URL. So, do I need to consider using POST method instead of GET.
It wouldn't really pass as 100% true to REST but you can have one web method that you call that looks at query string part of the url to get the additional parameters.
You would have a web method with a route of '/GetData'.
domain.com/GetData?Parameters=firstParm=1^secondParm=info^thirdParm=test
then in the web method, you would check the query string for Parameters and then split the string by the '^' symbol.
or
domain.com/GetData?firstParm=1&secondParm=info&thirdParm=test
this you would have to do a query string for each parameter.

Capturing an incoming webforms request in an MVC route

I'm converting a legacy webforms app to MVC, working through it a page at a time. To make the project easier to work with I've moved all the webforms pages, which were previously in the route of the project into a /webforms subdirectory. So I need to capture any incoming requests for /page.aspx?param=123 and redirect them to /webforms/page.aspx?param=123. I thought an easy way to do this would be to setup a route handler that passed any such requests to a controller that does that job. I set up a route like so:
routes.MapRoute("WebformsRedirect", "{*page}",
new { controller = "Webforms", action = "ForwardToPage" },
new { page = #"\S+.aspx\S*" }
);
This kind of works but it doesn't capture the query string, only the page part. I can get the query string for the Request object in the controller so it's not a huge deal but it would be nice to be able to do it through the route only. My routing unit tests (which I copied from Steve Sanderson's MVC book) actually pass correctly when I test them with querystrings so I'm confused why it isn't working. Is my regular expression wrong? They aren't my strong point.
QueryStrings are not part of the routing
if you requested for example "Home/Index?foo=bar" and you have a route that match "Foo/Bar" to Controller Foo , Action Bar without any more routing info (don't know anything about foo) you still can write
class HomeController: Controller {
ActionResult Index(string foo) {
}
}
now foo variable will equal bar , why ?
because its the model binder that gets the value of the parameters passed.
the model binder check 4 repositories by default QueryString , Routing Place Holders ,FormsCollections and Files
so what i am trying to say , the route and QueryStrings are two different things , it doesn't need to capture it

Getting to the query string (GET request array) inside a web service in .NET

I'm looking to find a way to access the .net query string contained in the standard ASP.NET request object inside a web service. In other words if I set a SOAP web service to this url:
http://localhost/service.asmx?id=2
Can I access the ID Get variable?
I just looked for "Request" of the context in asmx file and I saw that. But I'm not sure if it is right.
this.Context.Request.QueryString["id"];
HttpContext.Current.Request.QueryString["id"]
Since you ask, I guess there is no HttpContext.Current.Request ?
While searching for the solution of the same problem i decided to take different approach.
My query string was packed with lots of variables and since I was not able to access query string data from the web service, and I also did not want to send each query string variable as a separate parameter, I prepared my web method to expect one aditional string parameter.
That parameter was window.location (entire url of the page) in my javascript function on .aspx page
Once I had url in my web service, the rest was quite stright forward
Uri myRef = new Uri(stringMyWindowLocationParameter);
System.Collections.Specialized.NameValueCollection mojQuery = HttpUtility.ParseQueryString(myRef.Query);
Now my query string is contained inside myRef object and this is how I call it
// Instead trying to request query string like this
string myId = HttpContext.Current.Request.QueryString["id"];
// ... I called it like this
string myId = myRef["id"];
Maybe it's not the most elegant way but it solved my problem.

Resources