c# - Asp.net web api routing issue? -
I am using Web API in my MVC application. I have a problem when calling the Web API using the Jquey.get function. Consider the following scenario:
I have 2 controller names:
- Home MVC controller <
- Li> / li>
TestApi controller has 1 operation
- Received
When I am indicating the homemcc controller The view was on >
http: // localhost: 1025 / home
and when I call
$ Get the expected Jason response from the Get ("api / testAPI / get") browser.
But when I call homemcc controller ie
http: // localhost: 1025 / Home / About
I
$ Get the error from the browser ("api / testapi / get") , the resource is not found, and to find out what the resource is trying to do:
Http: // localhost: 1025 / home / about / api / testAPI / get
instead of
http: // localhost: 1025 / Why the About Home / was added to the API in the AP I / TestAPI / get
URL and why it was not added
Home / Index See when I was on the index view index see why API calls up Damage is working and why About look ?? You used to use a full URL.
You should use a full URL instead:
$ ("@ Url.RouteUrl" ("@ Url.RouteUrl" ("@ Url.RouteUrl" ("DefaultApi", New {httproute = "", Controller = "TestAPI", Action = "go"})) < / Pre>
If you want to keep your javascript in separate JS files (and not in razor files) then what you can do is an initial method called Razor View.
.js file:
var myPageJsUrls = {}; var MyPageInitialize = function (getItemsUrl, saveItemUrl, editItemUrl) {myPageJsUrls.getItemsUrl = getItemsUrl; myPageJsUrls.saveItemUrl = saveItemUrl; myPageJsUrls.editItemUrl = editItemUrl; } Var getItems = function () {return $ .get (myPageUrls.getItemsUrl);} ...
razor In the file:
& lt; scripts & gt; myPageInitialize ('@Lights "," items "),' @ Url.RouteUrl (" DefaultApi ", new { Httproute = "", Controller = "TestAPI"}, ...)
- Received
Comments
Post a Comment