开发者

ASP.NET 4 Routing hide id parameter

开发者 https://www.devze.com 2023-02-03 16:49 出处:网络
I am learning .net and building a site that uses url routing. I have it working although when attempting to get data from a database i am having to pass a ID parameter such as:

I am learning .net and building a site that uses url routing.

I have it working although when attempting to get data from a database i am having to pass a ID parameter such as:

services/{id}/{category}/{subcategory}

portfolio/{id}/{category}/{title}

blog/{date}/{id}/{title}

I have seen websites that do the same thing but they don't appear to be passing an ID parameter, how do they acheive this?

if they wanted the information on a particular service as per the link above would you just run an SQL query looking for a match on the subcategory name or for a blog post you look for a match on the blog title and date?

It just seems a bit strange 开发者_StackOverflow社区to me and if you have people adding blog posts etc there is always the slim possibility of more than one match.

Thanks for any help,

J.


You can always check the name of your blog post url before you release it. If it exists, just add a "_1" or something. In addition they mostly include the date.


In ASP.NET routing, Ids are most often passed in the url when you are using HttpGet protocol. This usually happens when you have a link:

<a href="tvShows/comedy/35">Watch The Big Bang Theory</a>

There's your id that you want to avoid.

You can also use HttpPost protocol and pass the values in form fields in the header (which is invisible to the casual user).

<form id="whatToDo">
    <input type="hidden" id="Category' value="tvShows">
    <input type="hidden" id="Genre' value="comedy">
    <input type="hidden" id="ShowId' value="35">
    <p>
        <input type="submit" value="Watch The Big Bang Theory" />
    </p>
</form>

So when the user clicks the button, your receiving controller will have three form fields, Category, Genre, and ShowId with the embedded values and they will not appear in the URL.

If you don't like the button, but need a link, you can always use javaScript/jQuery to submit the form.


As you state he sites are probably using something else as a unique parameter to identify objects to query. Maybe they are using a user name or id that is set on login behind the scenes to limit the sql query to only items that will apply to that user.


Normally you would maintain a title -> id relationship... so every time a blog is saved, updated or deleted a corresponding record in the title_id table is kept so a lookup can be done with just a single select-query.

0

精彩评论

暂无评论...
验证码 换一张
取 消