Not show data in URL ASP MVC4 - asp.net

I'm making a simple register login view profile application in MVC and when i call the action method of view profile i'm searching the user on the basis of his/her name.
Now my problem is username is visible in the url
like this
'http://localhost:23444/Home/EditProfile?Username=arrow'
Here arrow is my username.
Now i don't want that any details is shown in url like that
is there any way to do this
I've tried to change my RouteConfig.cs still doesn't work.
Here is my RouteConfig.cs file
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}",
defaults: new { controller = "Home", action = "Index" }
);
}
}
Please tell me how to solve this issue.
Thanks in advance.

If you have a search functionality, then this behavior is expected. If you just don't want to display it in URL, change the method of the dorm to POST. That should hide it from URL.
In case you have a link to other peoples' profiles, then you should follow #vikas_jagdale's answer. There a unique id is displayed in place of the name.
It would be best if you post the part of the code where you have this URL generated, that will help others to be specific.

You do not suppose to set username as a Id, Kindly follow below steps.
Create one unique column in DB with the type of unique identifier (GUID in C#).
Generate a new GUID during insert the same into DB with user details.
Now fetch the list of users with GUID column.
And now during creating a grid set your URL as given below -
#Url.Action("EditProfile", "Home", new { id = item.guidProp })

Related

Need direction about Controller, Views, Routing

I have 2 models.
Customer and Store Models.
A Customer can have many Stores.
I created basic/scaffolding controllers and views for both.
So Customer Controller has Index, Create, Edit, Delete actions.
In my index view for Customer, I want to have a link to "Stores" for each customer which will take to "Store" Index page, only that it will show a list for that client...
Now usually we have URL patterns such as
/Customer/Create
/Customer/Details/Id
/Customer/Edit/Id
As mentioned, I want a Stores link beside Edit/Delete/Details which will work something like,
/Customer/1/Store/Index
/Customer/1/Store/Create
/Customer/1/Store/Edit/1
My URL construction maybe wrong but that is my idea. How does one achieve this kind of behavior. Do I create a Store Function within the Customer Controller? I am confused on then how will I acheive /Customer/1/Store/Edit/1 this kind of behavior...
I am looking for reading material because I am unsure what to search for..Also, the URL pattern is not what I am looking for...I am looking for an easy way to implement this. Dont know if that makes sense..
The routing in asp.net mvc is quite flexible. With the template projects (and many samples) you get this route:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Giving you a url like /Customer/Edit/1 for controller "Customer", Action "Edit", id "1".
But you don't have to follow that pattern. So you can do something like:
routes.MapRoute(
name: "Store",
url: "Customer/{customerId}/Store/{action}/{storeId}",
defaults: new { controller = "Store", action = "Index", storeId = UrlParameter.Optional, customerId = 0 }
);
And then create an action method in your Store controller like:
public ActionResult Edit(int customerId, int storeId) { ... }
Letting you use the route /Customer/1/Store/Edit/3
Just remember that the first route that can possibly match will match, even if there is a better match defined later.

ASP.NET MVC 4 C# - Get Sectioned Elements or full URL to Parse

Being kind of a newb to MVC 4 (or really any of the MVC's for ASP.NET) I cant help but feel theres more to the URL helper than what I'm seeing.
Basically I've read the tutorials on populating the attributes in a controllers methods using a query string in the URL.
I dont liek query strings though and prefer a sectioned "folder" like style.
Without much further adu, this is the sample URL:
http://something.com/DataTypes/Search/searchString
this approach is actually pretty safe as there will only ever be single worded searches
I have tried in the DataTypes controller
[HttpGet]
public ActionResult Search(String q)
{
ViewBag.ProvidedQuery = q;
return View();
}
and a few other small variations, right now im just trying to get the string to show up in the view but I dont seem to be getting anything there.
Is there anyway to inject the 3rd string in the url into an attribute?
If not, which URL helper class am I supposed to use to acquire the string data in the URL? Even if I have to parse the whole URL manually so be it, i just need to acquire that 3rd element in the URL as a string
Extremely n00b question im sure, but either im not finding this simple guide somewhere, or im not searching google correctly...
What you're missing is that the default route parameter name is "id". So you want to do this:
[HttpGet]
public ActionResult Search(String id)
{
ViewBag.ProvidedQuery = id;
return View();
}
If you don't want to use the variable name id, then you can modify your Route to look like this:
routes.MapRoute(
name: "Search",
url: "DataTypes/Search/{searchString}",
defaults: new { controller = "DataTypes", action = "Search",
searchString = UrlParameter.Optional });
If you don't want the string to be optional, then you can remove the last field from the defaults object.
you can use RouteTable.Routes.GetRouteData(new HttpContextWrapper(httpContext)) to get the routedata
String URL to RouteValueDictionary
You need to look at the routing in the Global.asax.cs. For example for your case you could add a route to the routes collection like this:
routes.MapRoute("Search",
"DataTypes/Search/{q}",
new { controller = "DataTypes", action = "Search" }
);
Then the q parameter will automatically get mapped to your action. The default controller mapping is likely mapping it to "id".

MVC3 how to minimize the number of controllers

i am new to MVC3 and our project needs something like:
http://www.abc.com/product_1/product_1_subpage/...
http://www.abc.com/product_2/product_2_subpage/...
right now, i have product_1 as a controller; product_1_subpage as an action of this controller, however, think about i have over 100 different products, i cannot keep create over 100 controllers for each single product, i need to do something on this structure, any idea?
thank you very much for your help, really appreciate any input.
You would probably want to have only a single Controller called Products, for all your products, instead of having to add a new controller for every product.
With custom routes, or the default routing for that matter, you would still be able to generate individual links for individual products. Also, if you were to use your approach with a new controller for every product (which you really shouldn't!), you would have to re-compile and deploy your application every time you want to add another product - which would be a pain to maintain.
It sounds like you should have a look at the tutorials about MVC provided by the .Net team to get some basic understand of MVC, and how to think about it.
Use custom routes:
routes.MapRoute(
"ProductsRoute", // Route name
"products/{productName}/{subName}/{id}", // URL with parameters
new { controller = "Product", action = "View", id = UrlParameter.Optional } // Parameter defaults
);
That would make the following work:
public class ProductController : Controller
{
// http://yourweb/products/goggles/xray/Elite2000
public ActionResult View(string productName, string subName, string id)
{
}
}
how about changing the format of the url a little to take advantage of the routing:
http://www.abc.com/product/subpage/1

ActionLink 404 Error

Building my first ASP.NET MVC 3 application and trying to implement the ability to disassociate a given ice cream from a menu. Each has an integer identifier associated with it and on a page I display these mappings and provide a link by them to remove the ice cream from the menu.
I've got an ActionLink that looks like this:
#Html.ActionLink("Remove", "RemoveMenuIceCreamMapping", "IceCream", new { iceCreamId=item.IceCreamId, menuId=item.MenuId}, null)
In my IceCreamController I've got an Action that looks like this:
[HttpPost]
public PartialViewResult RemoveMenuIceCreamMapping(int iceCreamId, int menuId)
{
...
}
Did a little searching and believe I may need to modify the routes in the Global.asax.cs file's RegisterRoutes to handle these two parameters. So I tried this like so:
public static void RegisterRoutes(RoutesCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// I added this route in an attempt to handle the two parameters:
routes.MapRoute(
"RemoveMenuIceCreamMapping", // Route name
"IceCream/RemoveMenuIceCreamMapping/{iceCreamId}/{menuId}", // URLwith parameters
new
{
controller = "IceCream",
action = "RemoveMenuIceCreamMapping",
iceCreamId = UrlParameter.Optional,
menuId = UrlParameter.Optional
}
);
// this was there by default
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new {controller = "Home", action = "Index", id = UrlParameter.Optional }
};
}
But this doesn't work - I get a "The resource cannot be found." error, 404. Requested URL: /IceCream/RemoveMenuIceCreamMapping/1/10
1 is the Id of the IceCream and 10 is the menu's Id.
What I was expecting to happen was that the action RemoveMenuIceCreamMapping would get called, passing those two parameters, but I'm obviously not doing something right here and may just misunderstand how to accomplish what I want and be going about this the wrong way. Any guidance would be most appreciated.
Update
So, one more thing I've learned, after reading this SO question, my ActionLink isn't triggering a POST so removing the [HttpPost] from the action seemed like the right thing to do. And, in fact, as soon as I did that, the route was found and the action executed.
I think you problem is that the ActionLink uses an HTTP GET and you are only accepting HTTP POST.
You will probably need to change your view to issue an HTTP POST (e.g. with a regular HTML button inside a form) so that the verb that the browser sends matches with what you accept on the controller.

Question about custom routing in ASP.NET MVC

I have to admit in advance that I'm quite new to MVC, I've been going through the resources at www.asp.net/mvc, but I was wondering if you guys could help me with something.
I have been asked to create a ASP.NET version of an existing PHP website, this website has a huge number of existing links to it in a particular format, which I have to replicate in due to the amount of work to change all the existing links would be far too much.
The format of the existing links is;
/([A-Za-z0-9]{14})/([A-Za-z0-9_-]*)
My attempt at creating a custom route doesn't appear to be working. What I have done is change the RegisterRoutes method in Global.asax.cs file to be;
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"ExistingLink",
"{LinkId}/{Title}",
new {controller="ExistingLinkController", action="Index"},
new {LinkId = #"([A-Za-z0-9]{14})", Title = #"([A-Za-z0-9_-]*)"});
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
I have also created the 'ExistingLinkController' with an 'Index' action of;
public ActionResult Index(string LinkId, string Title)
{
ViewData["LinkId"] = LinkId;
ViewData["Title"] = Title;
return View();
}
And a view which contains the code;
<h2>LinkId: <%: ViewData["LinkId"] %>
</h2>Title: <%: ViewData["Title"] %></h2>
But when I try to go to;
/55a3ef90c4b709/This-is-just-a-test_0-9
I get the following error;
Description: HTTP 404. The resource
you are looking for (or one of its
dependencies) could have been removed,
had its name changed, or is
temporarily unavailable. Please
review the following URL and make sure
that it is spelled correctly.
Requested URL:
/55a3ef90c4b709/This-is-just-a-test_0-9
I was wondering whether anyone can see what I am doing wrong and possibly point me in the right direction, perhaps pointing at the bit of code that's wrong if its a simple problem or pointing me to a article that will help me get a better understanding if I've got the wrong end of the stick with this routing stuff.
Thanks for any help in advance
Satal :D
I think this:
new {controller="ExistingLinkController", action="Index"},
Should just be this:
new {controller="ExistingLink", action="Index"},
MVC adds the Controller part of the name itself - In the second route the controller is also called HomeController, but you only enter "Home" as the default for the controller argument.
when i get stuck with my routes i use the route debugger from Phil Hack
http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx
its just epic

Resources