ASP.NET MVC: ActionLink vs bare url - html

In ASP.NET MVC I see I have handy HTML helpers that I can use to construct form fields and any number of other little things. But then there's 'ActionLinks'.
Why use an ActionLink instead of just writing the darn url myself in an HTML anchor tag?
In other words, why would I use
<%: Html.ActionLink("Back to List", "QuantityTypes") %>
instead of just using plain ol' HTML and writing:
Back to List
Surely, I must get something extra with the ActionLink. I'm just missing it, right?

The action link will build you the proper URL based on the controller, action, areas, params, etc... It generates the URL based on the URL mapping rules defined in the your MVC routing system. It will map params to the correct url as well depending on if it needs to be included in the URL directly or via a querystring param.
Yes you could do it on your own and just type it all out but it builds the URL for you and ensures the URL that is generate is correct. It's a helper function... it helps you produce valid links :)
You should read Scott Guthrie's post and pay extra attention to the section "Constructing Outgoing URLs from the Routing System". It gives the why and explains other helpers that leverage the routing system.

You get centralized control of your URL's. So next time you need to change one for SEO purposes, you don't have to go searching for every place in the application, just switch it in the Global.asax.

What if you wanted to change the controller name from Internal to External. What is going to happen? You are going to need to change the href link by hand. ActionLink will do automatic routing. You don't need to mess with urls.

Another reason for using ActionLink over bare url is that you may need to expose a download link to a secured resource that can only be accessed by the application through identity impersonation.

Related

Multiple Alias Routes to base Razor Page

I'm pretty new to Razor Pages, and I'm trying to figure out how to replicate routing I have in my current Angular Page.
I have a base razor page that will be populated with different data depending on which parameter is passed to it. This is easy enough, and I know how to do this. However, my problem is in the routing because I want to be able to pass a readable parameter that is based off of the base URL. For example, I want to be able to do:
https://myURL/Band1
https://myURL/Band2
and have both point to the same page (but not the Index Page), consume the parameter "Band1" or "Band2" to display the associated information.
I understand how to consume the parameter, and how to get data, what I'm not clear on is how to do this routing based on the base URL. I can see how I'd do it if it were https://myURL/b/Band1 since I'd make a "b" page and accept parameters.
But how does one do this without that intervening segment of the URL? I need to be able to do this to not break existing links.
Thanks!
The docs for Razor Pages suggest you can create a page named Index.cshtml, which will act as the default where no page is specified in the URL.
Edit
If you want to preserve the parameterless index page, but have your page take its place when the additional URL part is provided, try the following in your page:
#page "/{bandName}"

HTML only way to add a new query string to an existing one

If I start out on the page example.com/example and click and a link <a href="?this">, I get sent to example.com/example?this. But if I start out on example.com/example?that and click <a href="?this">, ?this overwrites ?that and I get sent to example.com/example?this.
Is there an HTML only (no javascript) way to have a link send me to example.com/example?this&that if I start out on example.com/example?that but send me to example.com/example?this if I start out on example.com/example?
No, there isn't. You will have to use a server-side language or JavaScript to build the hrefs in order to achieve this.
The better question is whether it is really a good idea to heavily rely on query parameters or whether you could make use of some .htaccess URL rewriting (that is internally mapped to query parameters, but they will always be available as long as the URL structure is intact). You could also make use of session storage in your server-side language.
No, html urls are relative to the path you are currently on, not including query strings. You will need javascript.

can't write link to Get method using jsp

I wrote in my jsp page the next link:
Map
I want to send parameters to my controller (the controller is servlet).
BUT the problem is that in the browser it show's:
"http://localhost:8080/Attraxions4/Controller#/Attraxions4/Controller?pageName=OptionCategory&pageCommand=Map"
instade of:
"http://localhost:8080/Attraxions4/Controller?pageName=OptionCategory&pageCommand=Map"
what is this sign number #?
why it copy again the root folders?
what can i do to fix it ??
thank you!! :)
what is this sign number #?
It is a fragment identifier. It indicates the end of the part of the URL to send to the server and indicates the id of an element to scroll to.
It is often abused to store data for use with Ajax requests.
what can i do to fix it ?
Find the JavaScript you have that is hijacking the normal functionality of your links and remove it.

Use a System.Data.Linq.Binary object as an HTML image source

I'm using ASP.NET MVC and have a model that has System.Data.Linq.Binary property. The property represents a picture that has been stored in the database as an image column.
I am able to use the picture in my pages by setting up a separate controller action and using Response.OutputStream.Write to dump the Binary object and then setting the controller action as an HTML img source.
I'm wondering if there is any way to use a Binary object directly in a view without needing the separate controller action? The idea would be to achieve the below which I know will not work but it demonstrates what I'd like to be able to do.
<img src="<%= Model.MyBinaryProperty%>" />
By nature of the problem, no.
You can simulate it, but you will always be relying in a separate request that serves the image.
There are just too many options, some:
Use a regular asp.net handler
Only retrieve on the separate request vs. Store somewhere temporarily and serve from there during the request. Usually the earlier is best
Use a controller action
Depending on the load characteristics and only if really needed, serve the image download from a different server
Setup a route + routehandler that serves the image
Setup an action filter that by some convention handles serving the image without needing to explicitly define the separate action method.
I'm sure there are others ...

How can I post data (form) to html page and hijacking the data in the middle?

the site addres: http://www.ynet.co.il/YediothPortal/Ext/TalkBack/CdaTalkBack/1,2497,L-3650194-0-68-544-0--,00.html
fill the form with rubbish.
Hit 'Send'
the form post the data to another HTML without any parsing of the data i've just added
How do they do it?
A likely option is that they are using a content management system where "html" on the URL doesn't actually mean it's a static html file.
This may be out of left field, but I've certainly used the occasional JS function to grab everything in the header and either parse it or pass it to another script using AJAX.
I'll sometimes use this method in a 404.html page to grab the headers of the previous page, parse them out to see where someone was trying to go and redirect them.
That is, as annakata said, one of the numerous options available.
Edit based on clarified question:
Numerous frameworks can be configured to intercept an html request - for instance asp.net can be set to handle any given extension and an HTTPModule could do anything with that. It's really up to web server configuration what it decides to do with any request.
also: you don't really want to be saying "hijack"