How to have a profile url like facebook. (asp.net) - c#

HI i am new to aspdotnet and want to ask how do I able to have a facebook like profile link for each of the registered user in the database.
example:
https://www.facebook.com/james
As you can see after .com there is a unique name. but my question: Is that name a folder or some kind of auto generated link?? How can I implement this kind of link for each of the registered user in my database?
well i can easy do it wit GET but I want to hide the id from the url.

Take a look at your RouteConfig.cs file in your App_Start folder.
The default route (www.facebook.com) is set to your HomeController.Index() method. You can tell that by the "defaults" parameter.
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
In the example that you provided, "james" would be a string parameter for that Index method. Obviously, the signature to the method would be:
public ActionResult Index(string id) {
// Do stuff
}

Related

MVC Route Mapping

I have a website, xyz.com and users are setting up admin accounts where they then have other users that are registering under them. They call their account name wisconsinsponsor. Another user sets up another account called iowasponsor. So I want to be able to have the ability that a user could browse to xyz.com/wisconsinsponsor and xyz.com/iowasponsor and get funneled into the appropriate settings that these users have setup.
So then after I browse to xyz.com/wisconsinsponsor which will allow me to get the appropriate settings for wisconsinsponsor I can be dropped onto xyz.com/wisconsinsponsor/{controller}/{method}.
So I added the following code.
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
List<Sponsor> sponsors = new SponsorContext().Sponsors.ToList();
foreach (Sponsor sponsor in sponsors)
{
// ALL THE PROPERTIES:
// rentalProperties/
routes.MapRoute(
name: sponsor.SponsorName,
url: sponsor.SponsorName + "{controller}/{action}/{id}",
defaults: new
{
controller = "Home",
action = "Index",
id = sponsor.SponsorId
}
);
}
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new {
controller = "Home",
action = "Index",
id = UrlParameter.Optional }
);
}
So the main goal is that without logging in, I can get information that pertains to each "sponsor" and then just generic information if a user goes to 'xyz.com' without specifying a sponsor. The below works to a point for landing on the home page, but then when I navigate to login or any other view, I get for example 'xyz.com/[my first sponsor entry in the database]/admin/login' instead of 'xyz.com/admin/login'. Why doesn't the navigation fall to the Default route?
Change your route to simply include the sponsor, don't create individual routes for every sponsor, this is where routing becomes so powerful
routes.MapRoute(
name: null, //routes names must be unique, but you can have multiple named null (go figure)
url: "{sponsor}/{controller}/{action}/{id}",
defaults: new
{
sponsor = "defaultSponsor", //or whatever you want the default to be
controller = "Home",
action = "Index",
id = UrlParameter.Optional
}
);
Then, decorate each of your action methods with the sponsor argument
public ActionResult Index(string sponsor, int id) { }
Rereading your question tho, this doesn't help in the instance of not having a sponsor, unless your "defaultSponsor" is not really a sponsor, but your generic information that is presented. So when no sponsor is passed in the address bar to the routing, you see 'defaultSponsor' or and empty string and could then handle appropriately
Another reason to handle it this way, is RegisterRoutes is only called upon application start up, so if they were dynamically added while the app is running, they would be invalid routes until the application is restarted. By making it an argument, they would work dynamically as well.

Can I hide a part of a route?

I have an AppController and an AccountController. The AppController only has one view, index, which takes query string parameters from the id part of the url.
The default route is as follows: {controller}/{action}/{id}
This means for the query string parameters to work properly, the view name has to be in the url. url/view/id
I would like to hide that part of the url and render that view by default, so users need only go to url/id.
I have tried {controller}/{id} and {controller}/index/{id} but neither work.
I think this would work. Set the url as : "{controller}/{id}" and give it a default action parameter:
routes.MapRoute(
name: "Default",
url: "{controller}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

Different url from controllername in asp.net MVC

I have a controller called CarController located in a folder called Buy. So the url becomes www.website.com/Buy/Car
How do I make the url be instead "/purchase/vehicle" without changing controller and folder name?
Thanks!
You need to define a new route for it
routes.MapRoute(
name: "VehicleRoute",
url: "purchase/vehicle",
defaults: new { controller = "Car", action = "TheAction" }
);
Just make sure you have placed it before the default route.
You can do this with a custom route. See here for informations about routing. You can then create your custom route with default values for the Controller and the action, something like:
routes.MapRoute(
"MyRoute",
"purchase/vehicle",
defaults: new { controller = "Car", action = "Buy" }
);
You have to put in there the correct controller name and the action you want to call.

Understanding ASP.NET MVC Routes reg

I've created a system in MVC using the NerdDinner tutorial as a base to work off.
Everything was working fine until I used single action methods such as
Here is the global.asax.cs
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "mysample", id=UrlParameter.Optional }
);
which routes to
http:localhost/Home/mysample
i just want to create routes which has more than one action in the sense
http:localhost/<controller>/<action>/<params>
ex: localhost/mycontroller/myaction/details/myname
Any help much appreciated.
Thanks.
update 1:
i have writen router like this as
routes.MapRoute(
"myname", // Route name
"{controller}/{action}/{details}/{myname}", // URL with parameters
new { controller = "mycontroller", action = "myaction", details= "details", myname= "" } // Parameter defaults
);
and retried the value with following syntax as
String name=RouteData.Values["myname"].ToString();
it works fine .
but even though the url called as
localhost/mycontroller/myaction/details
its being routed to that controller and error is being thrown as null reference...
how to avoid it?
You can't define multiple actions in one MVC route.
In MVC routing configuration is used for mapping your Controlers and Actions to user friendly routes and:
Keep URLs clean
Keep URLs discoverable by end-users
Avoid Database IDs in URL
Understanding default route config:
routes.MapRoute(
name: "Default", // Route name
routeTemplate: "{controller}/{action}/{id}", // URL with parameters
defaults: new { controller = "Home", action = "mysample", id=UrlParameter.Optional }
);
The "routeTemplate" property on the Route class defines the Url
matching rule that should be used to evaluate if a route rule applies
to a particular incoming request.
The "defaults" property on the Route class defines a dictionary of
default values to use in the event that the incoming URL doesn't
include one of the parameter values specified.
Default route will map all requests, because it has defined default values for every property in routeTemplate, {} means that property is variable, if you not provide value for that param in URL, it will try to take default value if you provide it. In default route it has defined defaults for controller, action and id param is optional. That means if you have route like this:
.../Account/Login
It will take you to Account controller, Login action and because you didn't specified prop and it is defined as optional it will work.
.../Home
This will also work, and it will take you to Home contoller and mysample action
When you define custom route, like you did:
routes.MapRoute(
"myname", // Route name
"{controller}/{action}/{details}/{myname}", // URL with parameters
new { controller = "mycontroller", action = "myaction", details= "details", myname= "" } // Parameter defaults
);
You didn't specified myname as optional and you didn't specified it your route, that means that your URL: localhost/mycontroller/myaction/details wan't be handled by your custom route myname. It will be handled by default route. And when you try to access your myname param in controller it wan't be there and you will get null reference error. If you want to specifie default value of your parameter if not present in url you need to do that in your controller. For example:
public class MyController : Controller
{
public ActionResult MyAction(string details = "details", string myname = "")
{
...
and change your custom route to:
routes.MapRoute(
"myname", // Route name
"{controller}/{action}/{details}/{myname}", // URL with parameters
new { controller = "mycontroller", action = "myaction", details= UrlParameter.Optional, myname= UrlParameter.Optional } // Parameter defaults
);
But you can define only one controller and only one action, rest of the routeTemplate are parameters.
You can't define two action in one route. It make no sense.

MVC 3 How to use MapRoute

Could someone show me how to use the MapRoute method? I have tried creating my own routes, but it's not working. What i want to accomplish is a route that routes "http://servername/home/default.aspx" into controller "Home" and action "Default". Also, would it be possible to say that if the user is browsing the default.aspx "file", it would actually point to the "Index" action?
I have tried reading the MSDN references and googling, but it didn't make me any wiser.
Probably too late to help the developer who raised the question but may help someone else. New to MVC but what I found is the map routes seem to be processed in the order they are added. I had a similar problem, my specific route was not working until I started adding the default route as the last route.
If the default map route is added before your custom one and your custom URL matches the structure defined by the default map route you will never reach your custom route.
The route you want to configure the first part of your question is:
routes.MapRoute(
"",
"home/default.aspx",
new { controller = "Home", action = "Default" }
);
Assuming you wish to 'browse' default.aspx with some sort of parameter you can do something like:
routes.MapRoute(
"",
"home/default.aspx/{param}",
new { controller = "Home", action = "Default", param = UrlParameter.Optional }
);
And you would then need to create your Default action to accept string param.
You also have to make sure the parameter name is the same as the action's parameter name.
Example:
routes.MapRoute(
name: "MyName",
url: "{controller}/{action}/{myParam}",
defaults: new { controller = "MyController", action = "MyAction", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
MyController:
public ActionResult MyAction(string myParam = "")
{
}

Categories