Hide query string from URL asp.net c# - c#

I have this following url:
http://www.example.com?user=Ana
and I want to get http://www.example.com?Ana
How can I get it?

You can route example.com/Ana to Home/Index with parameter (you can change controller and action to what is needed). Just add new route to your routing dictionary
routes.MapRoute(
"UserPage",
"{controller}/{action}/{user}",
new { controller = "Home", action = "Index", user = "" }
);
You can read more about routing here at ASP.NET
NOTE:
as Alexei Levenkov said it requires .Net MVC to route it that way.

Related

Is it possible to have a parameter named action in MVC4?

PLEASE MARK AS DUPLICATE - Its already answered here
Is it possible to have a parameter named action in MVC4?
Trying to do this results in the parameter returning me the name of the controller action rather than the parameter value.
/Somecontroller/Someaction?action=value
When I try to access the parameter action, I get "Someaction" as the result rather than "value".
Trying to bind the parameter to a different name doesn't help either.
public ActionResult Someaction([Bind(Prefix = "action")] String id)
Edit: I have not found 'Action'/'action' in reserved MVC keywords either.
I haven't tested this, but I would assume you could try changing the url of your default route to something other than action
routes.MapRoute("Default", "{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional });
to
routes.MapRoute("Default", "{controller}/{cAction}/{id}",
new { controller = "Home", cAction = "Index", id = UrlParameter.Optional });
this should then allow you to use action as a parameter in your Action. I'm not certain why you'd want to do this, I would probably build a route url that accepted my action and built it as part of the url similar to this:
/SomeController/SomeAction/value where value is your "action" parameter.
EDIT based on comments:
I successfully created a route that goes to an aspx url'd route
routes.MapRoute(null, "third-party.aspx", new { controller = "Home", action = "Index" });
Obviously you can add whatever controller/action you want here and where you want to handle it, then you access that route via /third-party.aspx?action=value and it seemed to work for me

Parameters after url Asp net mvc

I would like to have some paramaters after my website's URl.
So, mysite.com/ThisIsMyStringMessage, would show this :
Hello, ThisIsMyStringMessage.
Of course, the view related to the action contains this :
Hello, #ViewBag.Message
And the HomeController :
public ActionResult Index(string message)
{
ViewBag.Message = message;
return View();
}
Edit related to comments :
#Zim, I'll have more than one controller.
#Jessee, So that's what I'm doing here, thank you, I didn't know really what I was doing.
With the default routing, ThisIsMyStringMessage will be picked up as the Controller, not as an Action method parameter. You will need a custom route to be able to do this. Try the following before the default route in Global.asax:
routes.MapRoute(
"RootWithParameter",
"{message}",
new { controller = "Home", action = "Index", message = UrlParameter.Optional }
);
Alternatively, access the method with mysite.com?message=ThisIsMyStringMessage
You will need to take a look at your RouteConfig.cs or Global.asax to setup a route with the parameter of message instead of id. Right now it probably looks like:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
You would need to change the url parameter to something like {controller}/{action}/{message} for that to work.
Also, be careful with passing strings in the URL. ASP.NET (and some modern browsers) gives you many security protections to prevent people from doing nasty stuff like XSS, but it's probably not a habit you want to create.

Action Link to "lose" parameter

I've got the following routes:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(null,"Conference/{shortName}/Submission/{submission}/{action}", new { controller = "Conference", action = "Show" });
routes.MapRoute(null,"Conference/{shortName}/{action}",new { controller = "Conference", action = "Index" });
routes.MapRoute("Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
The following (hopefully obvious) links which are all working:
/Conference/testconf
/Conference/testconf/ShowSubmissions
/Conference/testconf/Submission/firstSub
/Conference/testconf/Submission/firstSub/EditSubmission
When I'm now in Submission/firstSub and create a ActionLink like this
#Html.ActionLink("Show Submissions", "ShowSubmissions", "Conference", new { shortName = Model.confereceShortName },null)
it creates the following Link
/Conference/testconf/Submission/firstSub/ShowSubmissions
How can i let the actionlink forget about Submission/firstSub without hardcoding it there?
Where do you have a placeholder for {controller}?
The default route should look like the following sample.
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{*id}", // URL with parameters
new {controller = "Home", action = "Index", id = UrlParameter.Optional} // Parameter defaults
);
Also try to remove the /Submission/ part in your first route.
Links for posts on custom route creation and ordering:
1) Creating Custom Routes (C#)
2) Custom routing for ASP .NET MVC
3) official source from asp.net mvc
Sometimes searching for 30min isn't enough, you gotta search 2h...
ASP.NET MVC 2 RC2 Routing - How to clear low-level values when using ActionLink to refer to a higher level?
Routlink or delete the values in the constraints.
For me Routelink does the job.
Although Thanks
ElYusubov & Aleksey

How to specify controller in ActionLink html helper in C#

I have a web application developed in ASP.NET MVC3 with C# and Razor.
I would like to call a specific Action Method of a specific Controller by using the ActionLink HTML helper. I know that the second parameter of ActionLink specifies the Action Method to be called from the Default route, which is the only one in my Global.asax file:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Index", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
I would like to call the Download Action Method from the Home Controller instead of Index. This does not work:
#Html.ActionLink("Presentation", "Download", "Home", new { topicId = topic.TopicId } )
It requires as third parameter a type Object but I cannot find on the web any example.
What are the steps needed in order to call a specific Controller/ActionMethod? Shall I create another route in my Global.asas file?
Thanks
Try this one:
#Html.ActionLink("Download", "Download", new { controller = "Home", Id = topic.TopicId });
The third parameter, object: routeValues, is used as dictionary in Asp.net MVC. Phil Haacked blogged about the decision for using object as route values.
update:
Your overload function is not working because you are calling this method. String is also object. So, you are passing "Home" as routeValues and new { topicId = topic.Id} as htmlAttributes. :)
Is this the overload you require? You will need the 5th parameter for html attributes.
#Html.ActionLink("Presentation", "Download", "Home", new { topicId = topic.TopicId }, new { name="Download" )

MVC special routing

I have an asp.net MVC project where i need to define some custom routes. Similar to what you see for posts on Wordpress where the route is of the form postid-postname:
12-i-am-post
I know how to do something like postid/postname:
12/i-am-post.
But how do I make a route that combines the two, such as:
mywebsite.com/12-postname-is-her
routes.MapRoute(
"PostRoute", // Route name
"{controller}/{id}-{postName}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional, postName = UrlParameter.Optional } // Parameter defaults
);
public ActionResult Index(int id, string postName)
{
return View();
}
Should work for the following request http://localhost/Post/1-MyPostName
Since there are multiple hyphens you won't be able to use it as a delimiter/separator, unless you want it to always match the first occurrence. Your best bet is to do a catchall {*path} and parse the value in your controller.

Categories

Resources