Getting a url segment from a route - pyramid

For this route:
config.add_route('item_get', '/item/{id}', request_method='GET')
What is the easiest way to get the {id} part from a view that uses this route?

A dictionary is added to the request, called request.matchdict:
id_ = request.matchdict['id']

Related

Don't work pagination on new Instagram url-api for locations (?__a=1) (native_location_data)

Instagram began to give a new response to the request https://www.instagram.com/explore/locations/.../?__a=1 :
{"graphql":{"native_location_data:{...
old response is: {"graphql":{"location":{...
Pagination https://www.instagram.com/explore/locations/.../?__a=1&max_id=... don't work: Instagram responds as well as the first request (https://www.instagram.com/explore/locations/.../?__a=1)
P.S. Pagination for tags (with a new response) works correctly
P.P.S. Not all users have a new response to the request
The "native_location_data" response Object requires a different function to traverse. For example, if you want to catch all images, use this example:
sections = instagram_resp['native_location_data']['recent']['sections']
for sub_section in sections:
if sub_section['layout_type'] == 'media_grid':
for item in sub_section['layout_content']['medias']:
if item['media'].get('image_versions2'):
largest_res = sorted(
item['media']['image_versions2']['candidates'],
key=lambda o: o['height'] * o['width']
)[-1]['url']

append a value to existing url in reactjs

I have the following url and a parameter :
number:200
"http://localhost:8000/textpath"
I want the path to be like this:
"http://localhost:8000/textpath/200"
How do I do that using Reactjs?I want to use the appended url in fetch method as follows:
fetch("http://localhost:8000/textpath/200")
A simple append did the work.Doesn't have to complicate
fetch("http://localhost:8000/textpath/"+number)
Try using Template literals.
const url = `http://localhost:8000/textpath/${number}`
fetch(url);
where number = 200
Refer here for more information

pretty url search string instead of question marks

I have been looking at other websites and see when you search, primary something like a search term, location and category you will see a pretty url like:
example.com/black-boots/new-york/shoes
instead of what I have now which is something like:
example.com/search-results/search?=black+boots&city=new+york&category=shoes
In my route I could start with something like:
router.get('/search-results/:search/:city/:category', shopController.getSearchResults);
And in the controller I could use req.params.city and so on to get the values from the url but the part that I can't figure out is a good way to get the text input values into the url using a get request.
Using GET by default gives me the 'ugly' looking url.
Basically the part that needs to go into the form
<form method="GET" action="/search-results/search/city/category">
Comments, plus this code sample for a GET request:
const form = document.getElementById('searchform');
form.addEventListener('submit', evt => {
const who = encodeURIComponent(document.getElementById('who').value);
const where = encodeURIComponent(document.getElementById('where').value);
const what = encodeURIComponent(document.getElementById('what').value);
window.location.href = `/${who}/${where}/${what}`;
}

MVC5 with attribute routing: how to get route data associated with particular action

Let's assume I have an ActionDescriptor (or MethodInfo) object that points to some action method in my application. I want to get route table's entries (System.Web.Routing.Route objects) associated with this action.
Is there, by any chance, some framework method that might get me this information, or do I have to parse the route table somehow? How would you suggest to do this, in the second case?
That's how I did this:
var routeProvider = new DefaultDirectRouteProvider();
var routeEntries = routeProvider.GetDirectRoutes(
_actionDescriptor.ControllerDescriptor, new[] { _actionDescriptor }, new DefaultInlineConstraintResolver());

MVC5 Rewriting Routing Attribute - Default page

I am attempting to switch from RouteConfig to Routing Attributes.
I am following along the Pro ASP.NET MVC 5 book from Adam Freeman and I'm trying to convert the following code that handles the paging of clients.
routes.MapRoute(
name: null,
url: "{controller}/Page{page}",
defaults: new { action = "Index", status = (string)null },
constraints: new { page = #"\d+" }
);
This works great! As I go to different URLs, the links look very nice
http://localhost:65534/Client - Default page
http://localhost:65534/Client/Page2 - Second page
Now I've decided to try out Url Attributes and having a bit of problems when it comes to how 'pretty' the links are. All of the links are working fine, but it's the 'routing rewriting' that I am trying to fix.
Here are the important parts of my controller.
[RoutePrefix("Client")]
[Route("{action=index}/{id:int?}")]
public class ClientController : Controller {
[Route("Page{page:int?}")]
public ActionResult Index(string sortOrder, string search = null, int page = 1) {
With the attribute above the Index, going to /Client or to /Client/Page gives me a 404.
Adding a blank route to catch the default page
[Route("Page{page:int?}")]
[Route]
Works for /Client and /Client/Page3, but now the rewriting of the URL is messed up. Clicking on page 3 of the pager gives me a URL of
http://localhost:65534/Client?page=3
which is not what I want. Changing the routing to
[Route("Page{page:int?}")]
[Route("{page=1:int?}")]
Works almost 100%, but the default link for /Client is now
http://localhost:65534/Client/Page
So, I am now asking for help. How can I correctly convert the original MapRoute to the attributes?
Just use:
[Route("", Order = 1)]
[Route("Page{page:int}", Order = 2)]
UPDATE
Plainly and simply, the routing framework is dumb. It doesn't make decisions about which route is the most appropriate, it merely finds a matching route and returns. If you do something like:
Url.Action("Index", "Client", new { page = 1 })
You're expecting the generated URL to be /Client/Page1, but since you have a route where page is essentially optional, it always will choose that route and append anything it can't stuff into the URL as a querystring, i.e. /Client?page=1. The only way to get around this is to actually name the route you want and use that named route to generate the URL. For example:
[Route("", Order = 1)]
[Route("Page{page:int}", Name = "ClientWithPage", Order = 2)]
And then:
Url.RouteUrl("ClientWithPage", new { page = 1 })
Then, you'll get the route you expect because you're directly referencing it.
UPDATE #2
I'm not sure what you mean by "go into PagedList.MVC and add a name property to it". It doesn't require any core changes to the code because PagedList already has support for custom page links. Just change your pager code to something like:
#Html.PagedListPager((IPagedList)ViewBag.OnePageOfItems, page => Url.RouteUrl("ClientWithPage", new { page = page }))
And you'll get the URL style you want. Attribute routing can be a bit more finicky than traditional routing, but I'd hardly call it useless. It's far more flexible than traditional routing, but that flexibility has some costs.

Resources